AspNetCore配置多环境log4net配置文件

虚幻大学 xuhss 199℃ 0评论

Python微信订餐小程序课程视频

https://blog.csdn.net/m0_56069948/article/details/122285951

Python实战量化交易理财系统

https://blog.csdn.net/m0_56069948/article/details/122285941

前言

在之前的文章中有讲到AspNetCore多环境配置文件的应用,我们根据自己多种环境分别配置多个appsettings.$EnvironmentName.json文件。
在实际的开发中我们可能会遇到不只一个配置文件,如当我们使用log4net日志库时,喜欢使用单独的log4net.config配置文件。并且我们还遇到不同环境下的配置文件还存在差异。这时我们可能可以效仿appsettings.json多环境配置的风格实现多环境配置文件。

配置log4net

新建Web项目
7aec79adfb842c43e9c484a0728928db - AspNetCore配置多环境log4net配置文件
安装Microsoft.Extensions.Logging.Log4Net.AspNetCore
9835622375030b0296362e117802c4c2 - AspNetCore配置多环境log4net配置文件

创建两个环境的配置文件分别如图,并设置文件属性使其在发布时能够复制到根目录
6813e774e343ee5b7dade3000bb59abf - AspNetCore配置多环境log4net配置文件

两个配置文件内容如下,例如我们这里生产环境的配置文件多增加一个KafkaAppender使日志发送至kafka消息丢列中,然后kafka的消费者将日志消费至ES集群,而本地开发的日志则没必要进行上传ES。


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

  <root>
    Value of priority may be ALL, DEBUG, INFO, WARN, ERROR, FATAL, OFF.
    <priority value="ALL" />
    <appender-ref ref="error-file" />
    <appender-ref ref="debug-file" />
    <appender-ref ref="KafkaAppender" />
  root>

  <logger name="Common">
    <appender-ref ref="debugger"/>
    <priority value="DEBUG" />
  logger>

  <appender name="KafkaAppender" type="log4net.Kafka.Appender.KafkaAppender, log4net.Kafka.Appender">
    <KafkaSettings>
      <brokers>
        <add value="127.0.0.1:9092" />
      brokers>
      <topic type="log4net.Layout.PatternLayout">
        <conversionPattern value="kafka.logstash" />
      topic>
    KafkaSettings>
    <layout type="log4net.Layout.PatternLayout">
      <conversionPattern value="%date %level% [%t] %logger - %message" />
    layout>
  appender>

  <appender name="debugger" type="log4net.Appender.DebugAppender">

    <immediateFlush value="true" />
    <layout type="log4net.Layout.SimpleLayout" />
  appender>

  <appender name="debug-file" type="log4net.Appender.RollingFileAppender">
    <param name="Encoding" value="utf-8" />
    <file value="Logs/debug" />
    <appendToFile value="true" />

    <immediateFlush value="true" />
    <staticLogFileName value="false" />
    <rollingStyle value="Date" />
    <datepattern value="-yyyy.MM.dd'.log'" />

    <lockingModel type="log4net.Appender.FileAppender+MinimalLock" />
    <layout type="log4net.Layout.PatternLayout">
      <conversionPattern value="%date %level% [%property{trace}] %logger - %message%newline" />
    layout>
  appender>

  <appender name="error-file" type="log4net.Appender.RollingFileAppender">
    <param name="Encoding" value="utf-8" />
    <file value="Logs/error" />
    <appendToFile value="true" />

    <immediateFlush value="true" />
    <staticLogFileName value="false" />
    <rollingStyle value="Date" />
    <datepattern value="-yyyy.MM.dd'.log'" />

    <lockingModel type="log4net.Appender.FileAppender+MinimalLock" />
    <filter type="log4net.Filter.LevelRangeFilter">

      <levelMin value="ERROR" />
    filter>
    <layout type="log4net.Layout.PatternLayout">
      <conversionPattern value="%date [%t] %logger - %message [%P{Url}]%newline" />
    layout>
  appender>

log4net>

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

  <root>
    Value of priority may be ALL, DEBUG, INFO, WARN, ERROR, FATAL, OFF.
    <priority value="ALL" />
    <appender-ref ref="error-file" />
    <appender-ref ref="debug-file" />
  root>

  <logger name="Common">
    <appender-ref ref="debugger"/>
    <priority value="DEBUG" />
  logger>

  <appender name="debugger" type="log4net.Appender.DebugAppender">

    <immediateFlush value="true" />
    <layout type="log4net.Layout.SimpleLayout" />
  appender>

  <appender name="debug-file" type="log4net.Appender.RollingFileAppender">
    <param name="Encoding" value="utf-8" />
    <file value="Logs/debug" />
    <appendToFile value="true" />

    <immediateFlush value="true" />
    <staticLogFileName value="false" />
    <rollingStyle value="Date" />
    <datepattern value="-yyyy.MM.dd'.log'" />

    <lockingModel type="log4net.Appender.FileAppender+MinimalLock" />
    <layout type="log4net.Layout.PatternLayout">
      <conversionPattern value="%date %level% [%property{trace}] %logger - %message%newline" />
    layout>
  appender>

  <appender name="error-file" type="log4net.Appender.RollingFileAppender">
    <param name="Encoding" value="utf-8" />
    <file value="Logs/error" />
    <appendToFile value="true" />

    <immediateFlush value="true" />
    <staticLogFileName value="false" />
    <rollingStyle value="Date" />
    <datepattern value="-yyyy.MM.dd'.log'" />

    <lockingModel type="log4net.Appender.FileAppender+MinimalLock" />
    <filter type="log4net.Filter.LevelRangeFilter">

      <levelMin value="ERROR" />
    filter>
    <layout type="log4net.Layout.PatternLayout">
      <conversionPattern value="%date [%t] %logger - %message [%P{Url}]%newline" />
    layout>
  appender>

log4net>

Program.cs中添加log4net配置如下图

// ===============多环境log4net配置===============
IWebHostEnvironment environment = builder.Environment;
var configName = "log4net" + (environment.IsProduction() ? string.Empty : environment.EnvironmentName) + ".config";
builder.Logging.AddLog4Net(configName, watch: true);
// ===============多环境log4net配置===============

405ac62c2982b6b3fd270698a9ac1603 - AspNetCore配置多环境log4net配置文件
现在所有的配置都完成了。

转载请注明:xuhss » AspNetCore配置多环境log4net配置文件

喜欢 (0)

您必须 登录 才能发表评论!