英文:
Config file setting for Multi appenders(file and app insight) with different log level
问题
我有记录到文件和应用洞察的不同日志级别的要求。我有以下配置文件设置,它在文件和应用洞察中都以DEBUG级别记录日志。
<?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>
我还尝试将这两个附加程序注册到根元素下,如下所示:
<root>
<level value="DEBUG"/>
<appender-ref ref="ConsoleAppender"/>
<level value="ERROR"></level>
<appender-ref ref="ApplicationInsightAppender"/>
</root>
但仍然没有看到所需的结果。有人可以帮助吗?
英文:
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?
答案1
得分: 1
一个appender没有level
属性。
Log4net通过下面的消息通知了这一点。
log4net:ERROR XmlHierarchyConfigurator: 无法找到属性[level]以设置[log4net.Appender.ConsoleAppender]上的对象
要使一个appender以不同的级别记录日志,请使用filter。
下面的示例显示了如何配置ApplicationInsightAppender
仅记录错误及更高级别的日志。
<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>
完整示例,使ConsoleAppender
记录所有消息,而ApplicationInsightAppender
只从错误级别开始记录。
<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>
请注意,这些是XML配置文件的示例,用于配置Log4net日志记录器。
英文:
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>
</details>
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论