I have requirement to log to file as well as app insight with different log levels. I have below config file setting which logs with DEBUG level in both file as well as app insight.
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<configSections>
<section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler, log4net" />
</configSections>
<log4net>
<root>
<level value="DEBUG"/>
<appender-ref ref="ConsoleAppender"/>
</root>
<appender name="ConsoleAppender" type="log4net.Appender.ConsoleAppender">
<level value="DEBUG" />
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%utcdate [%thread] %-5level %logger - %message%newline" />
</layout>
</appender>
<appender name="ApplicationInsightAppender" type="Microsoft.ApplicationInsights.Log4NetAppender.ApplicationInsightsAppender, Microsoft.ApplicationInsights.Log4NetAppender">
<level value="ERROR" />
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%utcdate [%thread] %-5level %logger - %message%newline"/>
</layout>
</appender>
<logger name ="appinsightlogger">
<level value ="ERROR"></level>
<appender-ref ref="ApplicationInsightAppender"/>
</logger>
</log4net>
</configuration>
I also tried registering both the appenders under root element like below
<root>
<level value="DEBUG"/>
<appender-ref ref="ConsoleAppender"/>
<level value ="ERROR"></level>
<appender-ref ref="ApplicationInsightAppender"/>
</root>
But still did not see required results. Can anyone help on this?
An appender doesn't have a level
property.
Log4net notifies about this with below message.
log4net:ERROR XmlHierarchyConfigurator: Cannot find Property [level] to set object on [log4net.Appender.ConsoleAppender]
To have an appender log with a different level, use a filter.
Below example shows how to configure the ApplicationInsightAppender
to only log errors and upwards.
<appender name="ApplicationInsightAppender" type="Microsoft.ApplicationInsights.Log4NetAppender.ApplicationInsightsAppender, Microsoft.ApplicationInsights.Log4NetAppender">
<filter type="log4net.Filter.LevelRangeFilter">
<levelMin value="ERROR" />
<levelMax value="FATAL" />
</filter>
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%utcdate [%thread] %-5level %logger - %message%newline"/>
</layout>
</appender>
Full example, having the ConsoleAppender
log all messages and the ApplicationInsightAppender
only starting from error.
<log4net>
<root>
<level value="DEBUG"/>
<appender-ref ref="ConsoleAppender" />
<appender-ref ref="ApplicationInsightAppender" />
</root>
<appender name="ConsoleAppender" type="log4net.Appender.ConsoleAppender">
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%utcdate [%thread] %-5level %logger - %message%newline" />
</layout>
</appender>
<appender name="ApplicationInsightAppender" type="Microsoft.ApplicationInsights.Log4NetAppender.ApplicationInsightsAppender, Microsoft.ApplicationInsights.Log4NetAppender">
<filter type="log4net.Filter.LevelRangeFilter">
<levelMin value="ERROR" />
<levelMax value="FATAL" />
</filter>
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%utcdate [%thread] %-5level %logger - %message%newline"/>
</layout>
</appender>
</log4net>