log4net 如何从某些附加程序的代码更改附加程序的日志记录级别
log4net how to change logging level of appender from code of certain appender
我有 log4net.config 多个 appender
形成 C# 代码我想覆盖某些 appender
的日志记录的 minLvel
我的 appender 设置就像
<appender name="RollingLogFileAppender" type="log4net.Appender.RollingFileAppender" >
<file value="..\Logs\Api" />
<encoding value="utf-8" />
<appendToFile value="true" />
<datePattern value="_yyyyMMdd'.log'" />
<staticLogFileName value="false" />
<rollingStyle value="Composite"/>
<maxSizeRollBackups value="-1"/>
<maximumFileSize value="10MB"/>
<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="%d{yyyy-MM-dd HH:mm:ss.fff}	%p	%t	%property{Ip}	%property{Context}	%property{ContextId}	%c{1}	%m%n" />
</layout>
</appender>
以下是我目前的解决方案。
private static void SetFilterMinLogLevel(AppenderSkeleton appender, string inputLevel)
{
if (!String.IsNullOrWhiteSpace(inputLevel))
{
inputLevel = inputLevel.ToLower().Trim();
IFilter filterHead = appender.FilterHead;
LevelRangeFilter filterLevel = filterHead as LevelRangeFilter;
if (filterLevel == null)// 我的一个 appender 也有一个自定义过滤器
{
filterHead = appender.FilterHead.Next;
filterLevel = filterHead as LevelRangeFilter; ;
}
if (filterLevel != null)
{
switch (inputLevel)
{
case "info":
filterLevel.LevelMin = log4net.Core.Level.Info;//todo
break;
case "debug":
filterLevel.LevelMin = log4net.Core.Level.Debug;//todo
break;
case "warn":
filterLevel.LevelMin = log4net.Core.Level.Warn;//todo
break;
case "error":
filterLevel.LevelMin = log4net.Core.Level.Error;//todo
break;
case "fatal":
filterLevel.LevelMin = log4net.Core.Level.Fatal;//todo
break;
case "off":
filterLevel.LevelMin = filterLevel.LevelMax = log4net.Core.Level.Off;//trying to check switching off logs
break;
default:
Logger.DebugFormat("Unknown Log level {0}",inputLevel);
break;
}
}
}
}
我有 log4net.config 多个 appender 形成 C# 代码我想覆盖某些 appender
的日志记录的 minLvel我的 appender 设置就像
<appender name="RollingLogFileAppender" type="log4net.Appender.RollingFileAppender" >
<file value="..\Logs\Api" />
<encoding value="utf-8" />
<appendToFile value="true" />
<datePattern value="_yyyyMMdd'.log'" />
<staticLogFileName value="false" />
<rollingStyle value="Composite"/>
<maxSizeRollBackups value="-1"/>
<maximumFileSize value="10MB"/>
<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="%d{yyyy-MM-dd HH:mm:ss.fff}	%p	%t	%property{Ip}	%property{Context}	%property{ContextId}	%c{1}	%m%n" />
</layout>
</appender>
以下是我目前的解决方案。
private static void SetFilterMinLogLevel(AppenderSkeleton appender, string inputLevel)
{
if (!String.IsNullOrWhiteSpace(inputLevel))
{
inputLevel = inputLevel.ToLower().Trim();
IFilter filterHead = appender.FilterHead;
LevelRangeFilter filterLevel = filterHead as LevelRangeFilter;
if (filterLevel == null)// 我的一个 appender 也有一个自定义过滤器 { filterHead = appender.FilterHead.Next; filterLevel = filterHead as LevelRangeFilter; ;
}
if (filterLevel != null)
{
switch (inputLevel)
{
case "info":
filterLevel.LevelMin = log4net.Core.Level.Info;//todo
break;
case "debug":
filterLevel.LevelMin = log4net.Core.Level.Debug;//todo
break;
case "warn":
filterLevel.LevelMin = log4net.Core.Level.Warn;//todo
break;
case "error":
filterLevel.LevelMin = log4net.Core.Level.Error;//todo
break;
case "fatal":
filterLevel.LevelMin = log4net.Core.Level.Fatal;//todo
break;
case "off":
filterLevel.LevelMin = filterLevel.LevelMax = log4net.Core.Level.Off;//trying to check switching off logs
break;
default:
Logger.DebugFormat("Unknown Log level {0}",inputLevel);
break;
}
}
}
}