本文介绍了配置log4net写入不同的日志文件,没有配置文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想这样做:

配置Log4net以写入多个文件

但是我想使用代码配置.我该怎么做?

But I want to use code-as-configuration. How do I do this?

这是我尝试过的:

// Configure log A
Hierarchy hierarchy = (Hierarchy)LogManager.GetRepository();

PatternLayout patternLayout = new PatternLayout();
patternLayout.ConversionPattern = "%date %level %logger: %message%newline";
patternLayout.ActivateOptions();

RollingFileAppender appender = new RollingFileAppender();
appender.Name = @"MyLogA";
appender.File = @"C:\Temp\MyLogs\A.log";
appender.AppendToFile = true;
appender.MaxSizeRollBackups = 2;
appender.RollingStyle = RollingFileAppender.RollingMode.Size;
appender.MaximumFileSize = "10MB";
appender.Layout = patternLayout;
appender.LockingModel = new FileAppender.MinimalLock();
appender.StaticLogFileName = true;
appender.ActivateOptions();
hierarchy.Root.AddAppender(appender);

hierarchy.Configured = true;

// Configure log B
Hierarchy hierarchyB = (Hierarchy)LogManager.GetRepository();

PatternLayout patternLayoutB = new PatternLayout();
patternLayoutB.ConversionPattern = "%date %level %logger: %message%newline";
patternLayoutB.ActivateOptions();

RollingFileAppender appenderB = new RollingFileAppender();
appenderB.Name = @"MyLogB";
appenderB.File = @"C:\Temp\MyLogs\B.log";
appenderB.AppendToFile = true;
appenderB.MaxSizeRollBackups = 2;
appenderB.RollingStyle = RollingFileAppender.RollingMode.Size;
appenderB.MaximumFileSize = "10MB";
appenderB.Layout = patternLayout;
appenderB.LockingModel = new FileAppender.MinimalLock();
appenderB.StaticLogFileName = true;
appenderB.ActivateOptions();
hierarchyB.Root.AddAppender(appenderB);

hierarchyB.Configured = true;


// Test Log A

var loggerA = log4net.LogManager.GetLogger("MyLogA");

loggerA.Info("TestA");
loggerA.Info("TestA");
loggerA.Info("TestA");
loggerA.Info("TestA");
loggerA.Info("TestA");

// Test Log B
var loggerB = log4net.LogManager.GetLogger("MyLogB");
loggerB.Info("TestB");
loggerB.Info("TestB");
loggerB.Info("TestB");
loggerB.Info("TestB");
loggerB.Info("TestB");

但是,当我这样做时,所有十条日志消息都被转储到LogA和LogB中,它们不会转到各自的日志文件中.

But when I do this, all ten log messages get dumped into both LogA and LogB, they don't go to their respective individual log files.

如何使它正常工作?

推荐答案

我不完全理解为什么,但是以下内容解决了该问题:

I don't fully understand why, but the following fixes the problem:

var hierarchy = (Hierarchy)LogManager.GetRepository();
hierarchy.Threshold = Level.Debug;

// Configure LoggerA
string logNameA = @"A";
string fileNameA = @"C:\Temp\MyLogs\A.log";
var loggerA = hierarchy.LoggerFactory.CreateLogger("LoggerA");
loggerA.Hierarchy = hierarchy;
loggerA.AddAppender(CreateFileAppender(logNameA,fileNameA));
loggerA.Repository.Configured = true;
loggerA.Level = Level.Debug;

ILog logA = new LogImpl(loggerA);

// Configure LoggerB

string logNameB = @"B";
string fileNameB = @"C:\Temp\MyLogs\B.log";
var loggerB = hierarchy.LoggerFactory.CreateLogger("LoggerB");
loggerB.Hierarchy = hierarchy;
loggerB.AddAppender(CreateFileAppender(logNameB, fileNameB));
loggerB.Repository.Configured = true;
loggerB.Level = Level.Debug;

ILog logB = new LogImpl(loggerB);

// Test

logA.Error("logA");
logA.Error("logA");
logA.Error("logA");
logA.Error("logA");
logA.Error("logA");

logB.Error("logB");
logB.Error("logB");
logB.Error("logB");
logB.Error("logB");
logB.Error("logB");
logB.Error("logB");

CreateFileAppender()所在的位置:

private static IAppender CreateFileAppender(string name, string fileName)
{
    PatternLayout patternLayout = new PatternLayout();
    patternLayout.ConversionPattern = "%date %level %logger: %message%newline";
    patternLayout.ActivateOptions();

    RollingFileAppender appender = new RollingFileAppender();
    appender.Name = name;
    appender.File = fileName;
    appender.AppendToFile = true;
    appender.MaxSizeRollBackups = 2;
    appender.RollingStyle = RollingFileAppender.RollingMode.Size;
    appender.MaximumFileSize = "10MB";
    appender.Layout = patternLayout;
    appender.LockingModel = new FileAppender.MinimalLock();
    appender.StaticLogFileName = true;
    appender.ActivateOptions();
    return appender;
}

这篇关于配置log4net写入不同的日志文件,没有配置文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-19 02:57