log4net by code control output by Category

Scenario:

For example, our system has five tasks, each task is independent of the process, according to data the traditional way these processes will be output together, this will undoubtedly give us troubleshoot the problem more difficult, because what we need is each task a separate output file, such as task a is output to the log / a / log.log, task B is output to the log / b / log.log.

 

The above mentioned scene, said the following concept of a few log4net

Repository: Repository, represents a logging global object, we can be understood as an xml configuration file is usually used in mapping out a repository

Logger: logger, a plurality of library below logger, logging may be distinguished by names.

Appender: output source, a logger may have a plurality of output sources (such as: file, console, etc.)

 

The following talk about specific ideas: if we want to output different paths according to different tasks, theoretically define a global Repository, then there depending on the different tasks defined Logger, you can define one or more Appender under each Logger. But I look at the code, did not find out how to define multiple Logger in the Repository. So here I am on the use of alternative approaches each task to define a Repository.

LogHelper code is as follows:

 

public class LogHelper
{
    private static object lockObj = new object();
    private static Dictionary<string, ILog> diction = new Dictionary<string, ILog>();

    public static ILog GetLog(string logName)
    {
        if (!diction.ContainsKey(logName))
        {
            lock (lockObj)
            {
                if (!diction.ContainsKey(logName))
                {
                    var repositoryName = "rep" + logName;
                    var repository = LogManager.CreateRepository(repositoryName);
                    repository.ResetConfiguration();

                    //根据名称生成ILog对象
                    RollingFileAppender append = new RollingFileAppender();
                    append.Name = logName;
                    append.File = "logs/" + logName + "/log.log";
                    append.AppendToFile = true;
                    append.MaxSizeRollBackups = 100 ; 
                    append.MaximumFileSize = " 5MB " ; 
                    append.StaticLogFileName = to false ; 
                    append.LockingModel = new new FileAppender.MinimalLock (); 
                    append.RollingStyle = log4net.Appender.RollingFileAppender.RollingMode.Size;
                     String pattern = " Record time:% date% n thread ID: [% thread]% n log level:% - 5level% n recording position:% location% n message description:% property {message}% n exception:% exception% n message:% message % newline% n ------------------------------------------% n";
                    PatternLayout layout = new PatternLayout(pattern);
                    append.Layout = layout;
                    append.ActivateOptions();
                    log4net.Config.BasicConfigurator.Configure(repository, append);

                    diction.Add(logName, LogManager.GetLogger(repositoryName, logName));
                }

            }
        }
        return diction[logName];
    }
}

 

Instructions:

LogHelper.GetLog ( " the IIM " ) .DebugFormat ( " route request is: {0} " , path);

Description:
1. append.ActivateOptions (); this is to allow the current appender activated, otherwise it is not in force.
2. log4net.Config.BasicConfigurator.Configure (repository, append); append is arranged inside the repository

 

Guess you like

Origin www.cnblogs.com/duanjt/p/11103887.html