Log4Net 日志记录变量
Log4Net Logging Variables
log4net 是否可以记录自定义变量,或者我是否仅限于 "pre-configured" variables?例如
- %appdomain: the friendly name of the appdomain from which the log entry was made
- %date: the local datetime when the log entry was made
- %exception: a formatted form of the exception object in the log entry, if the entry contains an exception; otherwise, this format
expression adds nothing to the log entry
- etc
我需要记录会话信息,例如 ActionExecutedContext
。如果这是相关的,我从全局异常过滤器调用 log4net。
是否可能,如果可能,如何实现?
当前配置的 appender,仅使用前面提到的 "pre-configured" 个变量:
<appender name="ADONetAppender" type="log4net.Appender.ADONetAppender">
<bufferSize value="1" />
<connectionType value="LALALA DELETED" />
<commandText value="INSERT INTO Log ([Date],[Thread],[Level],[Logger],[Message],[Exception]) VALUES (@log_date, @thread, @log_level, @logger, @message, @exception)" />
<parameter>
<parameterName value="@log_date" />
<dbType value="DateTime" />
<layout type="log4net.Layout.RawTimeStampLayout" />
</parameter>
<parameter>
<parameterName value="@thread" />
<dbType value="String" />
<size value="255" />
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%thread" />
</layout>
</parameter>
<parameter>
<parameterName value="@log_level" />
<dbType value="String" />
<size value="50" />
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%level" />
</layout>
</parameter>
<parameter>
<parameterName value="@logger" />
<dbType value="String" />
<size value="255" />
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%logger" />
</layout>
</parameter>
<parameter>
<parameterName value="@message" />
<dbType value="String" />
<size value="4000" />
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%message" />
</layout>
</parameter>
<parameter>
<parameterName value="@exception" />
<dbType value="String" />
<size value="2000" />
<layout type="log4net.Layout.ExceptionLayout" />
</parameter>
</appender>
在 log4net 中,布局可以包含许多模式,即。 class在布局中遇到它们的字符串声明时可以输出一种特定信息的es。
例如布局中的 %logger
将解析为 LoggerPatternConverter
class,遇到时输出记录器名称。
PatternLayout
class:
中有所有可用模式的全局实例化
PatternLayout.s_globalRulesRegistry = new Hashtable(45);
PatternLayout.s_globalRulesRegistry.Add("literal", typeof(LiteralPatternConverter));
PatternLayout.s_globalRulesRegistry.Add("newline", typeof(NewLinePatternConverter));
PatternLayout.s_globalRulesRegistry.Add("n", typeof(NewLinePatternConverter));
PatternLayout.s_globalRulesRegistry.Add("c", typeof(LoggerPatternConverter));
但您可以通过配置向您的布局添加一些自定义模式。只需创建您自己的模式布局转换器:
public class ZoinxPatternlayoutConverter: PatternLayoutConverter
{
protected override void Convert(TextWriter writer, LoggingEvent loggingEvent)
{
writer.Write("Zoinx at {0:d}", DateTime.Now);
}
}
并将其添加到您的布局中:
<appender name="console" type="log4net.Appender.ConsoleAppender">
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%message %zoinx %newline" />
<converter>
<name value="zoinx"/>
<type value="App.ZoinxPatternLayoutConverter, App" />
</converter>
</layout>
</appender>
当然,在您的情况下,这可能有点多,如果您在每个布局中只使用一个 属性,例如 RawTimeStampLayout
,您可能会直接从布局派生而受益你有你的配置。或者,您可以继承 PatternLayout 以一劳永逸地注册您感兴趣的所有模式,并在您的配置中使用它们。
log4net 是否可以记录自定义变量,或者我是否仅限于 "pre-configured" variables?例如
- %appdomain: the friendly name of the appdomain from which the log entry was made
- %date: the local datetime when the log entry was made
- %exception: a formatted form of the exception object in the log entry, if the entry contains an exception; otherwise, this format expression adds nothing to the log entry
- etc
我需要记录会话信息,例如 ActionExecutedContext
。如果这是相关的,我从全局异常过滤器调用 log4net。
是否可能,如果可能,如何实现?
当前配置的 appender,仅使用前面提到的 "pre-configured" 个变量:
<appender name="ADONetAppender" type="log4net.Appender.ADONetAppender">
<bufferSize value="1" />
<connectionType value="LALALA DELETED" />
<commandText value="INSERT INTO Log ([Date],[Thread],[Level],[Logger],[Message],[Exception]) VALUES (@log_date, @thread, @log_level, @logger, @message, @exception)" />
<parameter>
<parameterName value="@log_date" />
<dbType value="DateTime" />
<layout type="log4net.Layout.RawTimeStampLayout" />
</parameter>
<parameter>
<parameterName value="@thread" />
<dbType value="String" />
<size value="255" />
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%thread" />
</layout>
</parameter>
<parameter>
<parameterName value="@log_level" />
<dbType value="String" />
<size value="50" />
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%level" />
</layout>
</parameter>
<parameter>
<parameterName value="@logger" />
<dbType value="String" />
<size value="255" />
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%logger" />
</layout>
</parameter>
<parameter>
<parameterName value="@message" />
<dbType value="String" />
<size value="4000" />
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%message" />
</layout>
</parameter>
<parameter>
<parameterName value="@exception" />
<dbType value="String" />
<size value="2000" />
<layout type="log4net.Layout.ExceptionLayout" />
</parameter>
</appender>
在 log4net 中,布局可以包含许多模式,即。 class在布局中遇到它们的字符串声明时可以输出一种特定信息的es。
例如布局中的 %logger
将解析为 LoggerPatternConverter
class,遇到时输出记录器名称。
PatternLayout
class:
PatternLayout.s_globalRulesRegistry = new Hashtable(45);
PatternLayout.s_globalRulesRegistry.Add("literal", typeof(LiteralPatternConverter));
PatternLayout.s_globalRulesRegistry.Add("newline", typeof(NewLinePatternConverter));
PatternLayout.s_globalRulesRegistry.Add("n", typeof(NewLinePatternConverter));
PatternLayout.s_globalRulesRegistry.Add("c", typeof(LoggerPatternConverter));
但您可以通过配置向您的布局添加一些自定义模式。只需创建您自己的模式布局转换器:
public class ZoinxPatternlayoutConverter: PatternLayoutConverter
{
protected override void Convert(TextWriter writer, LoggingEvent loggingEvent)
{
writer.Write("Zoinx at {0:d}", DateTime.Now);
}
}
并将其添加到您的布局中:
<appender name="console" type="log4net.Appender.ConsoleAppender">
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%message %zoinx %newline" />
<converter>
<name value="zoinx"/>
<type value="App.ZoinxPatternLayoutConverter, App" />
</converter>
</layout>
</appender>
当然,在您的情况下,这可能有点多,如果您在每个布局中只使用一个 属性,例如 RawTimeStampLayout
,您可能会直接从布局派生而受益你有你的配置。或者,您可以继承 PatternLayout 以一劳永逸地注册您感兴趣的所有模式,并在您的配置中使用它们。