If we need to configure different ilog, the process is this, first of all, to create a iloggerrepository, through which log level configuration, and a variety of appender, Then by Logmanager in Iloggerrepository get a Ilog object, you can write the log. The code is as follows:
Copy Code code as follows:
public static ILog GetLogger (String repositoryname = "")
{
if (string. IsNullOrEmpty (Repositoryname)) return Logmanager.getlogger ("Defalut");
Iloggerrepository repository = null;
Try
{
Repository = Logmanager.getrepository (repositoryname);
}
catch (Exception) {}
Find Direct return Ilog
if (repository!= null)
Return Logmanager.getlogger (Repositoryname, "Defalut");
//Not Found is created, multithreading is very likely to create when there is a
Try
{
Repository = logmanager.createrepository (repositoryname);
}
catch (Exception)
{
repository = logmanager.getrepository (repositoryname);
}
Configure log level read appsettings default configuration
var appset = ConfigurationManager.AppSettings.AllKeys;
Find Log Level
Const string loglevel = "LogLevel";
var hassettings = Array.indexof (Appset, loglevel);
if (Hassettings >-1)
{
var level = Configurationmanager.appsettings[loglevel]. ToLower ();
if (level = "All") repository. Threshold = Level.all;
else if (level = "Debug") repository. Threshold = Level.debug;
else if (level = = "Info") repository. Threshold = Level.info;
else if (level = = "Warn") repository. Threshold = Level.warn;
else if (level = "error") repository. Threshold = Level.error;
else if (level = = "Fatal") repository. Threshold = Level.fatal;
else if (level = "off") repository. Threshold = Level.off;
}
Else Repository. Threshold = Level.all;
Find Output Appender
Const string Logappender = "Logappender";
Hassettings = Array.indexof (Appset, Logappender);
if (Hassettings >-1)
{
var appenders = Configurationmanager.appsettings[logappender]. ToLower (). Split (', ');
foreach (Var appender in appenders)
{
if (Appender = = "Rollingfile") Loadrollingfileappender (repository);
else if (Appender = = "Console") Loadconsoleappender (repository);
else if (Appender = = "Trace") Loadtraceappender (repository);
}
}
else Loadrollingfileappender (repository);
Return Logmanager.getlogger (Repositoryname, "Default");
}
Log4net will automatically maintain ilog and iloggerrepository, so do not save, through the Logmanger access. But I didn't find a way to find out if the specified iloggerrepository existed, so I judged it through a try catch in the code above. Since we are configuring Log4net in code, but still want to set the log level and output type through configuration, I chose to configure it in the appsettings file, one at a time, for all iloggerrepository. The reason why it is not configured for each feature, mainly due to configuration problems, and if you really want to do so, directly in the log4net configuration of different logger, by loggername reference, may be better.
Log Maintenance
So the next question is, how do I delete a log?
Log4net can be configured Rollingfileappender, translated is scrolling file. You can do this by setting the maximumFileSize and maxsizerollbackups 2 parameters, which produce a new file each time it reaches a certain size, and the maximum number of files is maxsizerollbackups. But the premise is that the file name is consistent.
For example, we use the following configuration:
Copy Code code as follows:
<!--rollingfileappender: output to File-->
<appender name= "Sysappender" type= "log4net". Appender.rollingfileappender ">
Path to the <!--log-->
<file value= "logs/"/>
<!--overwrite, default is append true-->
<appendtofile value= "true"/>
<!--does not occupy the log file process-->
<lockingmodel type= "log4net. Appender.fileappender+minimallock "/>
<rollingstyle value= "Composite"/>
<!--file name-->
<datepattern value= "Yyyy-mm-dd HH" when. Log ' ></DatePattern>
<!--set Unlimited backup =-1, the maximum number of backups is 1000-->
<param name= "maxsizerollbackups" value= "1000"/>
<!--size of each file-->
<param name= "maximumFileSize" value= "500KB"/>
<!--name can be changed to false to change-->
<param name= "Staticlogfilename" value= "false"/>
<layout type= "log4net. Layout.patternlayout ">
<!--output format-->
<conversionpattern value= "%n" Record Time "%date%n" describes "%message%n"/>
</layout>
</appender>
The effect is that the log is in hours, the maximum number of backups per hour is 1000, and the log file is formed every 500kb. As the number of days increases, so does the file, and we need to clean it ourselves. So the above configuration can be slightly changed:
Copy Code code as follows:
<!--rollingfileappender: output to File-->
<appender name= "Sysappender" type= "log4net". Appender.rollingfileappender ">
Path to the <!--log-->
<file value= "logs/log. Log"/>
<!--overwrite, default is append true-->
<appendtofile value= "true"/>
<!--does not occupy the log file process-->
<lockingmodel type= "log4net. Appender.fileappender+minimallock "/>
<rollingstyle value= "Composite"/>
<!--file name-->
<datepattern value= "Yyyy-mm-dd HH" when. Log ' ></DatePattern>
<!--set Unlimited backup =-1, the maximum number of backups is 1000-->
<param name= "maxsizerollbackups" value= "1000"/>
<!--size of each file-->
<param name= "maximumFileSize" value= "500KB"/>
<!--name can be changed to false to change-->
<param name= "Staticlogfilename" value= "true"/>
<layout type= "log4net. Layout.patternlayout ">
<!--output format-->
<conversionpattern value= "%n" Record Time "%date%n" describes "%message%n"/>
</layout>
</appender>
The main is to set the log file name, and is a static name, do not allow changes. In this case, the log file is up to 1000, you do not have to manually maintain, you will see this log file name:
Log .log log .log.1 log .log.2 ... Default is file name the larger the last number, the oldest, so view the new log, mainly in the first file name.
also reminded: If the settings appender use a static filename, then the history of the log will be overwritten, and extremely concerned about the application of the history log, it is recommended not to use this scheme, clean up the log, or honestly write code.