写入两个位置不起作用
我觉得这应该是可能的,但我只能让 log4net 一次访问一个附加程序。
<?xml version="1.0" encoding="utf-8" ?>
<log4net debug="false">
<appender name="RollingLogFileAppender" type="log4net.Appender.RollingFileAppender">
<file value="C:\Logs\webclient\Admin.Web\logs\admin.log" />
<appendToFile value="true" />
<rollingStyle value="Date" />
<datePattern value="yyyyMMdd" />
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%date [%thread] %-5level %logger - %message%newline" />
</layout>
</appender>
<appender name="TraceAppender" type="log4net.Appender.TraceAppender">
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%date [%thread] %-5level %logger [%property{NDC}] - %message%newline" />
</layout>
</appender>
<!--Database appender-->
<appender name="AdoNetAppender" type="log4net.Appender.AdoNetAppender">
<bufferSize value="10" />
<connectionType value="System.Data.SqlClient.SqlConnection, System.Data, Version=2.0.0.0, Culture=neutral, PublicKeyToken=B77A5C561934E089" />
<connectionString value="Data Source=(localhost); Database=Error_Logs; Persist Security Info=True;User ID=;Password=" />
<commandText value="INSERT INTO [GeneralServiceLog] ([dtLog],[vchLevel],[vchLogger],[vchMessage],[vchException])
VALUES( @log_date, @log_level, @logger, @message, @ex)" />
<parameter>
<parameterName value="@log_date" />
<dbType value="DateTime" />
<layout type="log4net.Layout.PatternLayout" value="%date{yyyy'-'MM'-'dd HH':'mm':'ss'.'fff}" />
</parameter>
<parameter>
<parameterName value="@log_level" />
<dbType value="String" />
<size value="50" />
<layout type="log4net.Layout.PatternLayout" value="%level" />
</parameter>
<parameter>
<parameterName value="@logger" />
<dbType value="String" />
<size value="255" />
<layout type="log4net.Layout.PatternLayout" value="%logger" />
</parameter>
<parameter>
<parameterName value="@message" />
<dbType value="String" />
<size value="4000" />
<layout type="log4net.Layout.PatternLayout" value="%message" />
</parameter>
<parameter>
<parameterName value="@ex" />
<dbType value="String" />
<size value="4000" />
<layout type="log4net.Layout.ExceptionLayout" />
</parameter>
</appender>
<root>
<level value="ERROR" />
<appender-ref ref="AdoNetAppender" />
</root>
<Logger>
<level value="DEBUG" />
<appender-ref ref="RollingLogFileAppender" />
</Logger>
</log4net>
我希望将错误级别消息发送到数据库,将调试级别消息发送到文本文件,但所有消息都将写入文件。
我缺少什么?
谢谢!
I feel this should be possible but I can only get log4net to go to one appender at a time.
<?xml version="1.0" encoding="utf-8" ?>
<log4net debug="false">
<appender name="RollingLogFileAppender" type="log4net.Appender.RollingFileAppender">
<file value="C:\Logs\webclient\Admin.Web\logs\admin.log" />
<appendToFile value="true" />
<rollingStyle value="Date" />
<datePattern value="yyyyMMdd" />
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%date [%thread] %-5level %logger - %message%newline" />
</layout>
</appender>
<appender name="TraceAppender" type="log4net.Appender.TraceAppender">
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%date [%thread] %-5level %logger [%property{NDC}] - %message%newline" />
</layout>
</appender>
<!--Database appender-->
<appender name="AdoNetAppender" type="log4net.Appender.AdoNetAppender">
<bufferSize value="10" />
<connectionType value="System.Data.SqlClient.SqlConnection, System.Data, Version=2.0.0.0, Culture=neutral, PublicKeyToken=B77A5C561934E089" />
<connectionString value="Data Source=(localhost); Database=Error_Logs; Persist Security Info=True;User ID=;Password=" />
<commandText value="INSERT INTO [GeneralServiceLog] ([dtLog],[vchLevel],[vchLogger],[vchMessage],[vchException])
VALUES( @log_date, @log_level, @logger, @message, @ex)" />
<parameter>
<parameterName value="@log_date" />
<dbType value="DateTime" />
<layout type="log4net.Layout.PatternLayout" value="%date{yyyy'-'MM'-'dd HH':'mm':'ss'.'fff}" />
</parameter>
<parameter>
<parameterName value="@log_level" />
<dbType value="String" />
<size value="50" />
<layout type="log4net.Layout.PatternLayout" value="%level" />
</parameter>
<parameter>
<parameterName value="@logger" />
<dbType value="String" />
<size value="255" />
<layout type="log4net.Layout.PatternLayout" value="%logger" />
</parameter>
<parameter>
<parameterName value="@message" />
<dbType value="String" />
<size value="4000" />
<layout type="log4net.Layout.PatternLayout" value="%message" />
</parameter>
<parameter>
<parameterName value="@ex" />
<dbType value="String" />
<size value="4000" />
<layout type="log4net.Layout.ExceptionLayout" />
</parameter>
</appender>
<root>
<level value="ERROR" />
<appender-ref ref="AdoNetAppender" />
</root>
<Logger>
<level value="DEBUG" />
<appender-ref ref="RollingLogFileAppender" />
</Logger>
</log4net>
I want the ERROR level messages to go to the database and DEBUG level messages to go to the text file but all the messaging are just going to file.
What am I missing?
Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您需要按如下方式配置根记录器:
然后向数据库附加程序添加过滤器:
更新:
您的配置问题在于您将数据库附加程序分配给未命名的记录器,因此您的附加程序很可能永远不会被调用。在 log4net 中,您可以将附加程序分配给 记录器,但还没有达到水平。
You need to configure the root logger as follows:
then you add a filter to the database appender:
Update:
The problem with your configuration is that you assign the db appender to an unnamed logger and thus your appender in all likelihood never gets called. In log4net you can assign appenders to loggers, but not to levels.