为同一个日志记录器将两个不同级别的日志记录到两个不同的附加程序

我们有两个不同的 asp.net 应用程序,它们启用了 Log4net 日志记录。 它们都有相同的 Log4Net 版本,1.2.10.0。

我们已经将 log4net.Appender.AdoNetAppender日志记录器添加到这两个日志记录器中,并希望将根日志记录器的 Info 级别记录到它,但也希望将根日志记录器的错误级别记录到文件附加程序。 我们的配置如下;

<?xml version="1.0" encoding="utf-8"?>


<log4net>
<appender name="filelogAppender" type="log4net.Appender.RollingFileAppender" >
<file value="..\logs\app.debug.log" />
<encoding value="utf-8" />
<staticLogFileName value="true" />
<datePattern value=".yyyyMMdd.'log'" />
<rollingStyle value="Composite" />
<lockingModel type="log4net.Appender.FileAppender+MinimalLock" />
<appendToFile value="true" />
<maximumFileSize value="1MB" />
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%date [%3thread] %-5level %property{log4net:HostName} [%property{Revision}] %logger %message%n" />
</layout>
</appender>
<!--
use [DB]
GO
CREATE TABLE [dbo].[Log] (
[Id] [int] IDENTITY (1, 1) NOT NULL,
[Date] [datetime] NOT NULL,
[Application][varchar] (255) NOT NULL,
[Thread] [varchar] (255) NOT NULL,
[Level] [varchar] (50) NOT NULL,
[Logger] [varchar] (255) NOT NULL,
[Server][varchar](255) NOT NULL,
[Revision][varchar](50) NOT NULL,
[Message] [varchar] (4000) NOT NULL,
[Exception] [varchar] (2000) NULL
)
-->
<appender name="dbLogAppender" type="log4net.Appender.AdoNetAppender" xdt:Transform="InsertBefore(/log4net/root)">
<bufferSize value="100" />
<connectionType value="System.Data.SqlClient.SqlConnection, System.Data, Version=1.0.3300.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" />
<connectionString value="data source=sqlserver;initial catalog=DB;integrated security=false;persist security info=True;User ID=USER;Password=PASSWORD" />
<commandText value="INSERT INTO Log ([Date],[Application],[Thread],[Level],[Logger],[Server],[Revision],[Message],[Exception]) VALUES (@log_date,'WebApp1', @thread, @log_level, @logger, @server, @revision, @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="@server" />
<dbType value="String" />
<size value="255" />
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%property{log4net:HostName}"/>
</layout>
</parameter>
<parameter>
<parameterName value="@revision" />
<dbType value="String" />
<size value="50" />
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%property{Revision}"/>
</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>
<root>
<level value="Error"/>
<appender-ref ref="filelogAppender"/>
</root>
<root>
<level value="Info"/>
<appender-ref ref="dbLogAppender"/>
</root>
</log4net>

我们看到的问题是,对于一个应用程序,我们看到数据库中的条目,但对于另一个应用程序,我们看不到。

Revision是我们在两个应用程序的 Application_Start()中设置的 GlobalContext属性。这两种配置之间的唯一区别是 Application的硬编码值不同。

我们能有两个这样的根日志吗?这会是我们在一个应用程序中看到的问题的根源吗?

57060 次浏览

Two <root> elements are not allowed.

From documentation:

root     Optional element, maximum of one allowed. Defines the configuration of the root logger.

You can however have a root logger like you do, and then have a separate logger specified by a namespace.

<root>
<level value="Error"/>
<appender-ref ref="filelogAppender"/>
</root>
<logger name="MyCompany.MyApp.Namespace">
<level value="Info"/>
<appender-ref ref="dbLogAppender"/>
</logger>

You should be able to set the threshold property of each appender separately and include them in the same root.

<appender name="filelogAppender" type="log4net.Appender.RollingFileAppender">
<threshold value="Error" />
</appender>
<appender name="dblogAppender" type="log4net.Appender.AdoNetAppender">
<threshold value="Info" />
</appender>
<root>
<appender-ref ref="filelogAppender" />
<appender-ref ref="dblogAppender" />
</root>

reference

You could specify a LevelRangeFilter for each of the appenders, and define each appender in the root to log at different levels.

<appender name="filelogAppender" type="log4net.Appender.RollingFileAppender" >
<!--File Details/Layout Options-->
<filter type="log4net.Filter.LevelRangeFilter">
<levelMin value="INFO"/>
</filter>
</appender>
<appender name="dbLogAppender" type="log4net.Appender.AdoNetAppender">
<!--SQL Options-->
<filter type="log4net.Filter.LevelRangeFilter">
<levelMin value="ERROR"/>
</filter>
</appender>
<root>
<level value="INFO"/>
<appender-ref ref="filelogAppender" /> <!--Prints info, warn, error, or fatal logs. -->
<appender-ref ref="dbLogAppender" />   <!--Prints only error or fatal logs. -->
</root>

Simply use Evaluator, you do not need to give threshold in all the appenders.

<appender name="filelogAppender" type="log4net.Appender.RollingFileAppender">
<evaluator type="log4net.Core.LevelEvaluator">
<threshold value="ERROR" />
</evaluator>
</appender>