<log4net>
<logger name="LogManager2" additivity="false">
<level value="ALL"/>
<appender-ref ref="LogManager2" />
</logger>
<root>
<level value="ALL"/>
<appender-ref ref="LogManager1" />
</root>
<appender name="LogManager1" type="log4net.Appender.RollingFileAppender">
<file value="logs\log" />
<appendToFile value="true" />
<rollingStyle value="Composite" />
<datePattern value="_yyyy-MM-dd" />
<maxSizeRollBackups value="10" />
<maximumFileSize value="10MB" />
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%date %-5level - %message%newline" />
</layout>
</appender>
<appender name="LogManager2" type="log4net.Appender.RollingFileAppender">
<file value="logs\Logs2" />
<appendToFile value="true" />
<rollingStyle value="Composite" />
<datePattern value="_yyyy-MM-dd" />
<maxSizeRollBackups value="10" />
<maximumFileSize value="10MB" />
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%date %-5level - %message%newline" />
</layout>
</appender>
</log4net>
当我尝试将logger用于logmanager2时,
ILog Logger = LogManager.GetLogger(typeof(LogManager2));
应该将日志写入logs2文件,但是logger正在使用root日志文件并将数据记录到日志文件。我已经尝试了有关此主题的所有可用答案,但没有任何对我有用 任何人都可以解释我的问题吗?
答案 0 :(得分:1)
当您需要Logger时,您可以传递类型或字符串。
当您传递类型时,Log4net会搜索名称等于类名称的记录器,包括命名空间。
所以,你应该打电话:
ILog Logger = LogManager.GetLogger("LogManager2");
或将记录器重命名为YourNamespace.LogManager2
。就这样:
<logger name="LogManagerNamespace.LogManager2" additivity="false">
答案 1 :(得分:0)
我与此斗争,我不喜欢XML配置,所以这就是我写的(从多个不同的DLL工作,并为每个创建一个不同的日志):
using System;
using System.Reflection;
using log4net;
using log4net.Repository.Hierarchy;
using log4net.Core;
using log4net.Appender;
using log4net.Layout;
// ReSharper disable once CheckNamespace
namespace EasiPos.EasiLog
{
public class Log
{
private static ILog _log;
public static void Initialise(string logName, string logFile)
{
var hierarchy = (Hierarchy)LogManager.CreateRepository(logName);
var patternLayout = new PatternLayout {ConversionPattern = "%date %-5level - %message%newline%exception"};
patternLayout.ActivateOptions();
var rollingFileAppender = new RollingFileAppender
{
AppendToFile = true,
Name = logName,
File = logFile,
Layout = patternLayout,
MaxSizeRollBackups = 10,
RollingStyle = RollingFileAppender.RollingMode.Date,
StaticLogFileName = true
};
rollingFileAppender.ActivateOptions();
hierarchy.Root.AddAppender(rollingFileAppender);
hierarchy.Root.Level = Level.All;
hierarchy.Configured = true;
_log = LogManager.GetLogger(logName, logName);
}
public static void Info(string message)
{
_log.Info(message);
}
public static void Debug(string message)
{
_log.Debug(message);
}
public static void Error(string message)
{
_log.Error(message);
}
public static void Error(Exception exception)
{
_log.Error(exception.Message, exception);
}
public static void Error(string message, Exception exception)
{
_log.Error(message, exception);
}
}
}
初始化如下:
using static EasiPos.EasiLog.Log;
Initialise("EasiKDS", @"TRACE\EasiKDS.exe.log");