We have two different asp.net applications with Log4net logging enabled. They both have the same version of Log4Net, 1.2.10.0.
We have added the log4net.Appender.AdoNetAppender
logger to both of them and want to log Info level to it for the root logger, but also want to log to the Error level for a root logger to a file appender.
Our configuration is as follows;
<?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>
The issue we are seeing is that for one application we see entries in the DB, but for the other we do not.
Revision
is GlobalContext
property we set in Application_Start()
in both applications. And the only difference between the two configurations is that we have a different hardcoded value for Application
.
Can we have two root loggers like this? Could this be the source of our issue that we are seeing in one application?
You can't log to separate appenders - you need to configure different loggers, and attach the appropriate appender to each one. Then log different messages to the different loggers.
log4net doesn't support the concept of structured logging. Like shown in the conversionPattern element in the XML configuration, you have some variables to play with when writing to the storage. But including properties like FirstName in the Serilog example isn't available.
RollingFileAppender means the system creates a log file based on your filters, this way you can have log files based on dates (one file each day), or get the file splitted into small chunks when it hits certain size.
Async. This library provides asynchronous Log4Net logging. Application performance is improved by performing message appending I/O operations on one or more background threads.
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>
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>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With