1
我沒有成功嘗試這樣做:How do I filter on a custom level in log4net自定義log4net的過濾級別不寫日誌
下面是我使用的代碼:
public static class SpecialLogging
{
public static readonly log4net.Core.Level specialLevel = new log4net.Core.Level(35000, "SPECIAL");
public static void Special(this ILog log, string message)
{
log.Logger.Log(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType,
specialLevel, message, null);
}
}
這裏是我測試它:
private static void Main()
{
//Logger
LogManager.GetRepository().LevelMap.Add(SpecialLogging.specialLevel);
ILog log = LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
log.Special("Special");
}
最後在這裏我log4net.config文件:
<log4net>
<appender name="RollingLogFileAppenderDebug" type="log4net.Appender.RollingFileAppender">
<file value="./Logs/DEBUG.log" />
<appendToFile value="false" />
<rollingStyle value="Size" />
<maxSizeRollBackups value="3" />
<maximumFileSize value="5GB" />
<lockingModel type="log4net.Appender.FileAppender+MinimalLock" />
<filter type="log4net.Filter.LevelRangeFilter">
<acceptOnMatch value="true" />
<levelMin value="DEBUG" />
<levelMax value="FATAL" />
</filter>
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%date{G} [%thread] (%stacktrace) %5level - %message%newline" />
</layout>
</appender>
<appender name="RollingLogFileAppender" type="log4net.Appender.RollingFileAppender">
<file value="./Logs/Special.log" />
<appendToFile value="false" />
<rollingStyle value="Size" />
<maxSizeRollBackups value="3" />
<maximumFileSize value="5GB" />
<lockingModel type="log4net.Appender.FileAppender+MinimalLock" />
<filter type="log4net.Filter.LevelMatchFilter">
<acceptOnMatch value="true" />
<levelToMatch value="SPECIAL" />
</filter>
<filter type="log4net.Filter.DenyAllFilter" />
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%date{G} [%thread] (%stacktrace) %5level - %message%newline" />
</layout>
</appender>
<!-- Set the default logging level and add the active appenders -->
<root>
<level value="ALL" />
<appender-ref ref="RollingLogFileAppender" />
<appender-ref ref="RollingLogFileAppenderDebug" />
</root>
</log4net>
特殊消息出現在Debug.log文件中,但Special.log文件爲空!
非常感謝! –