1. 程式人生 > 其它 >AspNetCore配置多環境log4net配置檔案

AspNetCore配置多環境log4net配置檔案

前言

在之前的文章中有講到AspNetCore多環境配置檔案的應用,我們根據自己多種環境分別配置多個appsettings.$EnvironmentName.json檔案。
在實際的開發中我們可能會遇到不只一個配置檔案,如當我們使用log4net日誌庫時,喜歡使用單獨的log4net.config配置檔案。並且我們還遇到不同環境下的配置檔案還存在差異。這時我們可能可以效仿appsettings.json多環境配置的風格實現多環境配置檔案。

配置log4net

新建Web專案

安裝Microsoft.Extensions.Logging.Log4Net.AspNetCore

建立兩個環境的配置檔案分別如圖,並設定檔案屬性使其在釋出時能夠複製到根目錄

兩個配置檔案內容如下,例如我們這裡生產環境的配置檔案多增加一個KafkaAppender使日誌傳送至kafka訊息丟列中,然後kafka的消費者將日誌消費至ES叢集,而本地開發的日誌則沒必要進行上傳ES。

<!--log4net.config-->
<?xml version="1.0" encoding="utf-8" ?>
<log4net>
  <!-- If you are looking here and want more output, first thing to do is change root/priority/@value to "INFO" or "ALL". -->
  <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>

  <!-- Example of turning on the output from a component or namespace. -->
  <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">
    <!-- Sends log messages to Visual Studio if attached. -->
    <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" />
    <!-- Immediate flush on error log, to avoid data loss with sudden termination. -->
    <immediateFlush value="true" />
    <staticLogFileName value="false" />
    <rollingStyle value="Date" />
    <datepattern value="-yyyy.MM.dd'.log'" />
    <!-- Prevents Orchard.exe from displaying locking debug messages. -->
    <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" />
    <!-- Immediate flush on error log, to avoid data loss with sudden termination. -->
    <immediateFlush value="true" />
    <staticLogFileName value="false" />
    <rollingStyle value="Date" />
    <datepattern value="-yyyy.MM.dd'.log'" />
    <!-- Prevents Orchard.exe from displaying locking debug messages. -->
    <lockingModel type="log4net.Appender.FileAppender+MinimalLock" />
    <filter type="log4net.Filter.LevelRangeFilter">
      <!-- Only ERROR and FATAL log messages end up in this target, even if child loggers accept lower priority. -->
      <levelMin value="ERROR" />
    </filter>
    <layout type="log4net.Layout.PatternLayout">
      <conversionPattern value="%date [%t] %logger - %message [%P{Url}]%newline" />
    </layout>
  </appender>

</log4net>

<!--log4net.Development.config-->
<?xml version="1.0" encoding="utf-8" ?>
<log4net>
  <!-- If you are looking here and want more output, first thing to do is change root/priority/@value to "INFO" or "ALL". -->
  <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>

  <!-- Example of turning on the output from a component or namespace. -->
  <logger name="Common">
    <appender-ref ref="debugger"/>
    <priority value="DEBUG" />
  </logger>
  
  <appender name="debugger" type="log4net.Appender.DebugAppender">
    <!-- Sends log messages to Visual Studio if attached. -->
    <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" />
    <!-- Immediate flush on error log, to avoid data loss with sudden termination. -->
    <immediateFlush value="true" />
    <staticLogFileName value="false" />
    <rollingStyle value="Date" />
    <datepattern value="-yyyy.MM.dd'.log'" />
    <!-- Prevents Orchard.exe from displaying locking debug messages. -->
    <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" />
    <!-- Immediate flush on error log, to avoid data loss with sudden termination. -->
    <immediateFlush value="true" />
    <staticLogFileName value="false" />
    <rollingStyle value="Date" />
    <datepattern value="-yyyy.MM.dd'.log'" />
    <!-- Prevents Orchard.exe from displaying locking debug messages. -->
    <lockingModel type="log4net.Appender.FileAppender+MinimalLock" />
    <filter type="log4net.Filter.LevelRangeFilter">
      <!-- Only ERROR and FATAL log messages end up in this target, even if child loggers accept lower priority. -->
      <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配置===============


現在所有的配置都完成了。