多附加器(文件和应用洞察)的配置文件设置,具有不同的日志级别。

huangapple go评论56阅读模式
英文:

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.

&lt;?xml version=&quot;1.0&quot; encoding=&quot;utf-8&quot; ?&gt;
&lt;configuration&gt;
  &lt;configSections&gt;
    &lt;section name=&quot;log4net&quot; type=&quot;log4net.Config.Log4NetConfigurationSectionHandler, log4net&quot; /&gt;
  &lt;/configSections&gt;
  &lt;log4net&gt;
    &lt;root&gt;
      &lt;level value=&quot;DEBUG&quot;/&gt;
      &lt;appender-ref ref=&quot;ConsoleAppender&quot;/&gt;
    &lt;/root&gt;
    &lt;appender name=&quot;ConsoleAppender&quot; type=&quot;log4net.Appender.ConsoleAppender&quot;&gt;
      &lt;level value=&quot;DEBUG&quot; /&gt;
      &lt;layout type=&quot;log4net.Layout.PatternLayout&quot;&gt;
        &lt;conversionPattern value=&quot;%utcdate [%thread] %-5level %logger - %message%newline&quot; /&gt;
      &lt;/layout&gt;
    &lt;/appender&gt;
    &lt;appender name=&quot;ApplicationInsightAppender&quot; type=&quot;Microsoft.ApplicationInsights.Log4NetAppender.ApplicationInsightsAppender, Microsoft.ApplicationInsights.Log4NetAppender&quot;&gt;
      &lt;level value=&quot;ERROR&quot; /&gt;
      &lt;layout type=&quot;log4net.Layout.PatternLayout&quot;&gt;
        &lt;conversionPattern value=&quot;%utcdate [%thread] %-5level %logger - %message%newline&quot;/&gt;
      &lt;/layout&gt;       
    &lt;/appender&gt;
    &lt;logger name =&quot;appinsightlogger&quot;&gt;
      &lt;level value =&quot;ERROR&quot;&gt;&lt;/level&gt;
      &lt;appender-ref ref=&quot;ApplicationInsightAppender&quot;/&gt;
    &lt;/logger&gt;
  &lt;/log4net&gt;
&lt;/configuration&gt;

I also tried registering both the appenders under root element like below

&lt;root&gt;
  &lt;level value=&quot;DEBUG&quot;/&gt;
  &lt;appender-ref ref=&quot;ConsoleAppender&quot;/&gt;
  &lt;level value =&quot;ERROR&quot;&gt;&lt;/level&gt;
  &lt;appender-ref ref=&quot;ApplicationInsightAppender&quot;/&gt;
&lt;/root&gt;

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.

&lt;appender name=&quot;ApplicationInsightAppender&quot; type=&quot;Microsoft.ApplicationInsights.Log4NetAppender.ApplicationInsightsAppender, Microsoft.ApplicationInsights.Log4NetAppender&quot;&gt;
  &lt;filter type=&quot;log4net.Filter.LevelRangeFilter&quot;&gt;
    &lt;levelMin value=&quot;ERROR&quot; /&gt;
    &lt;levelMax value=&quot;FATAL&quot; /&gt;
  &lt;/filter&gt;
  &lt;layout type=&quot;log4net.Layout.PatternLayout&quot;&gt;
    &lt;conversionPattern value=&quot;%utcdate [%thread] %-5level %logger - %message%newline&quot;/&gt;
  &lt;/layout&gt;       
&lt;/appender&gt;

Full example, having the ConsoleAppender log all messages and the ApplicationInsightAppender only starting from error.

&lt;log4net&gt;
  &lt;root&gt;
    &lt;level value=&quot;DEBUG&quot;/&gt;
    &lt;appender-ref ref=&quot;ConsoleAppender&quot; /&gt;
    &lt;appender-ref ref=&quot;ApplicationInsightAppender&quot; /&gt;
  &lt;/root&gt;

  &lt;appender name=&quot;ConsoleAppender&quot; type=&quot;log4net.Appender.ConsoleAppender&quot;&gt;
    &lt;layout type=&quot;log4net.Layout.PatternLayout&quot;&gt;
      &lt;conversionPattern value=&quot;%utcdate [%thread] %-5level %logger - %message%newline&quot; /&gt;
    &lt;/layout&gt;
  &lt;/appender&gt;

  &lt;appender name=&quot;ApplicationInsightAppender&quot; type=&quot;Microsoft.ApplicationInsights.Log4NetAppender.ApplicationInsightsAppender, Microsoft.ApplicationInsights.Log4NetAppender&quot;&gt;
    &lt;filter type=&quot;log4net.Filter.LevelRangeFilter&quot;&gt;
      &lt;levelMin value=&quot;ERROR&quot; /&gt;
      &lt;levelMax value=&quot;FATAL&quot; /&gt;
    &lt;/filter&gt;
    &lt;layout type=&quot;log4net.Layout.PatternLayout&quot;&gt;
      &lt;conversionPattern value=&quot;%utcdate [%thread] %-5level %logger - %message%newline&quot;/&gt;
    &lt;/layout&gt;       
  &lt;/appender&gt;
&lt;/log4net&gt;
  
  

</details>



huangapple
  • 本文由 发表于 2023年7月10日 18:33:06
  • 转载请务必保留本文链接:https://go.coder-hub.com/76652893.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定