我试图使用log4net在两个不同的表中创建日志。第一个adonetappender将登录一个名为' log'并且应该是标准的id,日期,线程,级别等等。第二个adonetappender需要登录到一个表,其中包含三个名为' usageLog'并且只需要记录日期/时间和两个额外的字符串值。我不能为我的生活弄清楚。我需要做什么?到目前为止,下面是我的配置。我不确定它是否正确,我不知道如何在程序中使用它。
<log4net>
<logger additivity="false" name="Log">
<level value="INFO"/>
<appender-ref ref="LogAppender" />
</logger>
<logger additivity="false" name="UsageLog">
<level value="INFO"/>
<appender-ref ref="UsageLogAppender" />
</logger>
<appender name="LogAppender" type="log4net.Appender.AdoNetAppender">
<bufferSize value="1" />
<!-- Causes errors to be written immediately - default is 100 -->
<connectionType value="System.Data.SqlClient.SqlConnection, System.Data, Version=1.0.3300.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" />
<connectionString value="data source=dummy;initial catalog=dummy;integrated security=false;persist security info=True;User ID=dummy;Password=dummy" />
<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>
<appender name="UsageLogAppender" type="log4net.Appender.AdoNetAppender">
<bufferSize value="1" />
<!-- Causes errors to be written immediately - default is 100 -->
<connectionType value="System.Data.SqlClient.SqlConnection, System.Data, Version=1.0.3300.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" />
<connectionString value="data source=dummy;initial catalog=dummy;integrated security=false;persist security info=True;User ID=dummy;Password=dummy" />
<commandText value="INSERT INTO UsageLog ([Date],[ManDesc],[ModDesc]) VALUES (@log_date, @man_desc, @model_desc)" />
<parameter>
<parameterName value="@log_date" />
<dbType value="DateTime" />
<layout type="log4net.Layout.RawTimeStampLayout" />
</parameter>
<parameter>
<parameterName value="@man_desc" />
<dbType value="String" />
<size value="255" />
<layout type="log4net.Layout.RawPropertyLayout">
<conversionPattern value="%property{man_desc}" />
</layout>
</parameter>
<parameter>
<parameterName value="@model_desc" />
<dbType value="String" />
<size value="255" />
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%property{model_desc}" />
</layout>
</parameter>
</appender>
答案 0 :(得分:1)
在我看来,您希望将相同的消息记录到具有不同列的两个不同表中。如果这是真的,您似乎错过了根记录器:
<root>
<level value="INFO" />
<appender-ref ref="LogAppender" />
<appender-ref ref="UsageLogAppender" />
</root>
但是,如果您在代码中声明这样的记录器,并且只希望那些特定的记录器记录到这些不同的故事表:
private readonly static ILog log = LogManager.GetLogger("Log");
private readonly static ILog log = LogManager.GetLogger("UsageLog");
通过在代码中以这种方式声明您的记录器,您的上述配置仍应有效。如果您没有以这种方式声明日志,则不会记录任何内容。打开log4net日志记录总是有帮助的: