log4Net - 记录器只使用根附加器,而不是其他附加器
log4Net - logger only using root appender, not other appender
<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>
当我尝试为 logmanager2 使用记录器时,
ILog Logger = LogManager.GetLogger(typeof(LogManager2));
它应该将日志写入 logs2 文件,但记录器正在使用根日志文件并将数据记录到日志中 file.I 已经尝试了有关此主题的所有可用答案,但对我没有任何效果
谁能给我解释一下这个问题?
当你需要一个记录器时,你可以传递一个类型或一个字符串。
当您传递一个类型时,Log4net 会搜索名称等于 class 名称的记录器,包括命名空间。
所以,您应该调用:
ILog Logger = LogManager.GetLogger("LogManager2");
或将记录器重命名为 YourNamespace.LogManager2
。像这样:
<logger name="LogManagerNamespace.LogManager2" additivity="false">
我为此苦苦挣扎,我不喜欢 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");
<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>
当我尝试为 logmanager2 使用记录器时,
ILog Logger = LogManager.GetLogger(typeof(LogManager2));
它应该将日志写入 logs2 文件,但记录器正在使用根日志文件并将数据记录到日志中 file.I 已经尝试了有关此主题的所有可用答案,但对我没有任何效果 谁能给我解释一下这个问题?
当你需要一个记录器时,你可以传递一个类型或一个字符串。
当您传递一个类型时,Log4net 会搜索名称等于 class 名称的记录器,包括命名空间。
所以,您应该调用:
ILog Logger = LogManager.GetLogger("LogManager2");
或将记录器重命名为 YourNamespace.LogManager2
。像这样:
<logger name="LogManagerNamespace.LogManager2" additivity="false">
我为此苦苦挣扎,我不喜欢 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");