使用依赖注入在 Windows 服务中实现 log4net [英] Implement log4net in a windows service using dependency injection

查看:47
本文介绍了使用依赖注入在 Windows 服务中实现 log4net的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果在每 10 分钟运行一次的 Windows 服务的日志文件中发生错误,我想写入错误.我正在使用 .net 框架 4.6 并在 autofac 中使用依赖注入.我想使用 Microsoft.Extensions.Logging dll.

解决方案

我在 Windows 服务中使用 ASP.Net 4.8 和用于 DI 的 Autofac 执行此操作.我也不必在 AssemblyInfo.cs 中添加任何程序集信息,因为我将配置信息放在 app.config 中,而不是单独的配置文件中.

app.config 把它放在 app.config 文件的顶部,紧跟在

之后<预><代码><配置><configSections><部分名称=log4net"type="log4net.Config.Log4NetConfigurationSectionHandler, log4net";/></configSections>

log4net 配置 把它放在 app.config 文件的底部,就在 </configuration> 里面.它将打印到您的 Visual Studio 输出窗口和名为 myservice.log 的文件

<根><级别值=所有"/><appender-ref ref=控制台"/><appender-ref ref="文件";/></root><附加程序名称=控制台"type="log4net.Appender.ConsoleAppender"><布局类型=log4net.Layout.PatternLayout"><conversionPattern value="%date %level %logger - %message%newline"/></布局></appender><附加程序名称=文件"type=log4net.Appender.RollingFileAppender"><文件值=myservice.log"/><appendToFile 值=真"/><rollingStyle 值=尺寸"/><maxSizeRollBackups 值=5"/><maximumFileSize 值=10MB"/><staticLogFileName value="true";/><布局类型=log4net.Layout.PatternLayout"><conversionPattern value="%date [%thread] %level %logger - %message%newline";/></布局></appender></配置>

Program.cs

static void Main(){ContainerBuilder cb = new ContainerBuilder();cb.RegisterType().AsSelf().InstancePerLifetimeScope();log4net.Config.XmlConfigurator.Configure();ServiceBase.Run(container.Resolve());}

MyService.cs

private readonly ILog _logger;公共 MyService(ILog 记录器){初始化组件();_logger = 记录器;_logger.Info("Hello World!");}

I want to write error if occur in log file in a windows service which runs in every 10 minutes. I am using .net framework 4.6 and using Dependency Injection with autofac. I want to use Microsoft.Extensions.Logging dll.

解决方案

I did this in a Windows Service, with ASP.Net 4.8, and Autofac for DI. I also didn't have to add any assembly info in AssemblyInfo.cs, because I put the config info in the app.config, instead of a separate config file.

app.config put this at the top of your app.config file just after <configuration>

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

log4net configuration put this at the bottom of your app.config file, just inside </configuration>. It will print to your visual studio output window and to a file called myservice.log

<log4net>
    <root>
      <level value="ALL" />
      <appender-ref ref="console" />
      <appender-ref ref="file" />
    </root>
    <appender name="console" type="log4net.Appender.ConsoleAppender">
      <layout type="log4net.Layout.PatternLayout">
        <conversionPattern value="%date %level %logger - %message%newline" />
      </layout>
    </appender>
    <appender name="file" type="log4net.Appender.RollingFileAppender">
      <file value="myservice.log" />
      <appendToFile value="true" />
      <rollingStyle value="Size" />
      <maxSizeRollBackups value="5" />
      <maximumFileSize value="10MB" />
      <staticLogFileName value="true" />
      <layout type="log4net.Layout.PatternLayout">
        <conversionPattern value="%date [%thread] %level %logger - %message%newline" />
      </layout>
    </appender>
  </log4net>
</configuration>

Program.cs

static void Main()
{
            ContainerBuilder cb = new ContainerBuilder();
            cb.RegisterType<MyService>().AsSelf().InstancePerLifetimeScope();
            log4net.Config.XmlConfigurator.Configure();
            ServiceBase.Run(container.Resolve<MyService>());
}

MyService.cs

private readonly ILog _logger;
public MyService(ILog logger)
{
    InitializeComponent();
    _logger = logger;
    _logger.Info("Hello World!");
}

这篇关于使用依赖注入在 Windows 服务中实现 log4net的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆