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

标签 c# windows service log4net-configuration

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

[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 的步骤吗?我需要创建哪些文件;我需要添加什么配置;我知道我需要创建一个记录器类,但配置让我完全困惑。

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

步骤 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 main 中添加了这个:

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 步) 在我登录的类(class)中,我添加了以下内容:

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 文件。同样,优先考虑是使用该属性还是程序集属性来加载配置。

关于c# - 在 C# 服务应用程序中配置 log4net 的步骤,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42568524/

相关文章:

c# - 用 Mono 编译 csharp 项目

java - 如何让 RequireJS 优化器将日志结果输出到文件?

windows - Windows 客户端上的 SharePoint 站点单点登录

function - Angular 5 服务在函数内部显然为空

Angular 2 - 在 Promise 中更改模型时 GUI 不更新

java - 使用什么设计模式来实现事务或链接机制

c# - 使用 autofac 键控服务在运行时基于通用接口(interface)解析类型

c# - 如果匿名类型对象不可枚举,它如何转换为字符串?

linux - 从Docker容器访问网络驱动器

android - 从通知启动服务