在 C# 服务应用程序中配置 log4net 的步骤

Steps to configure log4net in a C# service application

我对如何在 Windows 服务项目中配置 log4net 感到非常困惑。除此之外,我是具有 Visual Studio 配置的新手。所以,我创建了这个 Windows 服务应用程序,我想向它添加 log4net。所有说明都不同,一个答案据说包括装配信息的以下行。另一个答案是将其添加到 AssemblyInfo.cs(在您的 App_Code 文件夹中)。那么这是否意味着我需要在我的源文件夹中添加一个名为 AssemblyInfo.cs 的 class,然后在构造函数中添加这一行? 我不知道那是什么意思!

[assembly:log4net.Config.XmlConfigurator(ConfigFile = "log4net.config", Watch = true)]

我已将 appSettings 添加到我的 app.config 文件中:

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
    <startup> 
        <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5" />
    </startup>
    <appSettings>
        <add key="log4net.Internal.Debug" value="true"/>
    </appSettings> 
</configuration>

我还需要创建一个 log4net.config 文件吗?

我尝试按照 this questions 的说明进行操作,但没有足够的信息让我理解。我花了几个小时寻找有意义的说明。有人可以告诉我(高级别)为 Windows 服务应用程序配置 log4net 的步骤吗?我需要创建哪些文件;我需要添加什么配置;我知道我需要创建一个记录器 class 但配置让我完全困惑。

--------------------------------编辑------ --------------------------

步骤 1) 我的 app.config 看起来像这样:

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
    <startup> 
        <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5" />
    </startup>

    <configSections>
      <section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler,Log4net"/>
    </configSections>

    <log4net>
      <appender name="LogFileAppender" type="log4net.Appender.RollingFileAppender">
        <file value="log.log" />
        <appendToFile value="true" />
        <rollingStyle value="Size" />
        <maxSizeRollBackups value="5" />
        <maximumFileSize value="100KB" />
        <staticLogFileName value="true" />
        <layout type="log4net.Layout.PatternLayout">
          <conversionPattern value="%date %level %logger - %message %exception%newline" />
        </layout>
      </appender>
      <root>
        <level value="ALL" />
        <appender-ref ref="LogFileAppender" />
      </root>
    </log4net>

</configuration>

步骤 2) 我已将此添加到 Program.cs 主要部分:

using System;
using System.Collections.Generic;
using System.Linq;
using System.ServiceProcess;
using System.Text;
using System.Threading.Tasks;

[assembly: log4net.Config.XmlConfigurator(Watch = true)]

namespace MyAppService
{
    static class Program
    {
        /// <summary>
        /// The main entry point for the application.
        /// </summary>
        static void Main()
        {
            log4net.Config.XmlConfigurator.Configure();

步骤 3) 我添加了对 log4net 的引用

步骤 4) 在我登录的 classes 中,我添加了这个:

private static ILog logger = LogManager.GetLogger(typeof(Reader));

步骤 4) 我添加了这样的日志记录语句:

logger.Info("Data Read Completed Successfully.");

您可以使用 app.config 进行配置:对于 Windows 服务来说,有一个单独的文件是一种偏好,而不是必需的,因为该服务可以观察配置文件的变化。

关于任何地方的汇编指令,您可以将它添加到任何文件,只要它在启动项目中——您的服务——尽管通常将它们添加到现有的 AssemblyInfo.cs 文件中。

还必须在启动例程中调用 Log4net,as the documentation says in bold:

Using attributes can be a clearer method for defining where the application's configuration will be loaded from. However it is worth noting that attributes are purely passive. They are information only. Therefore if you use configuration attributes you must invoke log4net to allow it to read the attributes. A simple call to LogManager.GetLogger will cause the attributes on the calling assembly to be read and processed. Therefore it is imperative to make a logging call as early as possible during the application start-up, and certainly before any external assemblies have been loaded and invoked.

为此,您的服务启动代码必须包含如下一行:

LogManager.GetLogger("initialise logging system");

或者,您可以删除属性,只在启动程序中调用 XmlConfigurator.ConfigureAndWatch(),默认情况下将加载并监视 app.config 文件。同样,是使用那个属性还是程序集属性来加载配置,这是一个偏好问题。