C # makes exception logging through log4net
The exception record in C # also has a template, which is log4net. Many will not say the direct look how to use it. 1, quote Log4net.dll.
2, the realization Log4net method.
public class Logimplement
{
Private ILog logger;
Public logimplement (ILog log)
{
This.logger = log;
}
public void Debug (object message)
{
This.logger.Debug (message);
}
public void Debug (object message, Exception e)
{
This.logger.Debug (message, E);
}
public void warming (object message)
{
This.logger.Warn (message);
}
public void warming (Object message,exception e)
{
This.logger.Warn (message, E);
}
public void Error (object message)
{
This.logger.Error (message);
}
public void Error (object message, Exception e)
{
This.logger.Error (message, E);
}
public void Info (object message)
{
This.logger.Info (message);
}
public void Info (object message, Exception e)
{
This.logger.Info (message, E);
}
}
public class Logfactory
{
Static Logfactory ()
{
FileInfo configfile = new FileInfo (AppDomain.CurrentDomain.BaseDirectory + @ "Log.config");
Log4net. Config.XmlConfigurator.Configure (ConfigFile);
}
public static logimplement GetLogger (type type)
{
return new Logimplement (Logmanager.getlogger (type));
}
public static logimplement GetLogger (String str)
{
return new Logimplement (Logmanager.getlogger (str));
}
}
3. Configuration files
<configuration>
<configSections>
<section name= "log4net" type= "log4net. config.log4netconfigurationsectionhandler,log4net-net-2.0 "/>
</configSections>
<log4net>
<root>
<appender-ref ref= "Errorlogfileappender"/>
<appender-ref ref= "Warningfileappender"/>
<appender-ref ref= "Debuglogfileappender"/>
</root>
<!--Records warning-->
<appender name= "Warningfileappender" type= "log4net. Appender.rollingfileappender ">
<file value= "Log\warn.log"/>
<appendtofile value= "true"/>
<maxsizerollbackups value= "Ten"/>
<maximumfilesize value= "1MB"/>
<rollingstyle value= "Size"/>
<staticlogfilename value= "true"/>
<layout type= "log4net. Layout.patternlayout ">
<conversionpattern value= "[%date] [%-5level] [%logger]-%message%newline"/>
</layout>
<filter type= "log4net. Filter.levelrangefilter ">
<param name= "Levelmin" value= "WARN"/>
<param name= "Levelmax" value= "WARN"/>
</filter>
</appender>
<!--record Error--
<appender name= "Errorlogfileappender" type= "log4net. Appender.rollingfileappender ">
<file value= "Log\error.log"/>
<appendtofile value= "true"/>
<maxsizerollbackups value= "Ten"/>
<maximumfilesize value= "1MB"/>
<rollingstyle value= "Size"/>
<staticlogfilename value= "true"/>
<layout type= "log4net. Layout.patternlayout ">
<conversionpattern value= "[%date] [%-5level] [%logger]-%message%newline"/>
</layout>
<filter type= "log4net. Filter.levelrangefilter ">
<param name= "Levelmin" value= "ERROR"/>
<param name= "Levelmax" value= "ERROR"/>
</filter>
</appender>
<!--record the debug log--
<appender name= "Debuglogfileappender" type= "log4net. Appender.rollingfileappender ">
<file value= "Log\debug.log"/>
<param name= "Appendtofile" value= "true"/>
<param name= "maxsizerollbackups" value= "ten"/>
<param name= "maximumFileSize" value= "1MB"/>
<param name= "Staticlogfilename" value= "true"/>
<param name= "Rollingstyle" value= "Size"/>
<layout type= "log4net. Layout.patternlayout ">
<param name= "Conversionpattern" value= "[%p] [%d{mm/dd/yyyy hh:mm:ss,fff}] [%t] [%c]-%m%n"/>
</layout>
<filter type= "log4net. Filter.levelrangefilter ">
<param name= "Levelmin" value= "DEBUG"/>
<param name= "Levelmax" value= "DEBUG"/>
</filter>
</appender>
<!--debug mode, output log to console--
<appender name= "Consoleappender" type= "log4net. Appender.coloredconsoleappender ">
<mapping>
<level value= "ERROR"/>
<forecolor value= "Red, highintensity"/>
<!--<backcolor value= "Green"/>-->
</mapping>
<mapping>
<level value= "DEBUG"/>
<forecolor value= "Yellow, highintensity"/>
<backcolor value= "Green"/>
</mapping>
<layout type= "log4net. Layout.patternlayout ">
<footer value= "--------------------------------------------------------------------"/>
<param name= "Conversionpattern" value= "[%p] [%d{mm/dd/yyyy hh:mm:ss,fff}] [%t] [%c]-%m%n"/>
</layout>
<filter type= "log4net. Filter.levelrangefilter ">
<param name= "Levelmin" value= "DEBUG"/>
<param name= "Levelmax" value= "ERROR"/>
</filter>
</appender>
</log4net>
<startup>
<supportedruntime version= "v4.0" sku= ". netframework,version=v4.0 "/>
</startup>
</configuration>
Where the exception file is more than 1M, the file is automatically added, the path to the exception file, and the. NET version to be supported. As for other properties in use, you can slowly realize.
4. Record exceptions
public class Recordlog
{
public static void Recordexception (Exception e)
{
Logimplement log = Logfactory.getlogger (typeof (Recordlog));
Log. Error (E.message + e.stacktrace);
}
}
5. Call
static void Main (string[] args)
{
Try
{
String test = "123TR";
int i = Int. Parse (test);//Create an exception
}
catch (Exception e)
{
Recordlog.recordexception (e);
}
}
"Turn" log4.net mark