基于log4net中的构建配置的不同追加程序 [英] Different appenders based on build configuration in log4net

查看:81
本文介绍了基于log4net中的构建配置的不同追加程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的主应用程序是基于某些指示符(启动标志,#if指令等)在控制台环境中启动或作为 Windows服务启动.只要应用程序以"生产模式"运行,例如没有用户上下文作为启动服务,我想同时登录文件(信息,警告)和 Windows事件日志.但是,在调试时,我只希望将日志消息发送到控制台窗口.

My main application either starts in a console environment or as a Windows Service, based on certain indicators (launch flags, #if directive,...). As long as the application runs in 'production mode', e.g. without a user context as a startup service, I want to log both in a file (info, warnings) as well as into the Windows event log. However when I'm debugging, I want log messages only been carried out to the console window.

有什么方法可以使用log4net来实现,而这不包括在运行时触摸应用程序配置吗?

Is there any way to achieve this using log4net that doesn't include touching the application configuration during runtime?

推荐答案

坦率地说,代码方法对我来说听起来不是一个好主意,因为它对最终将管理系统的人员隐藏了一些行为.由于您可能有两个构建配置(发布和调试?),为什么不使用它们来根据构建而更改配置文件您正在做.

Frankly the code approach doesn't sound like a very good idea to me, since it hides some behavior to the person that will manage the system in the end. Since you probably have two build configurations (release and debug?) why not use them instead to change the configuration file depending on the build you're doing.

如果您真的不想执行此操作,请按以下步骤操作:您可以在log4net库的全局上下文中设置自定义属性

If you really don't want to do this, then here is how to do it: you can set a custom property in the global context of your log4net library

// declare this code as soon as you've determined what context you're in
log4net.GlobalContext.Properties["ApplicationMode"] = "Service";
// or 'Console", with an #IF directive

然后在您的log4net配置中,您可以使用log4net的PropertyFilter过滤出将由附加程序捕获的消息

Then in your log4net configuration, you can filter out the messages that will be caught by your appenders by using the PropertyFilter of log4net

您的开发者的控制台附加程序:

The console appender for your dev:

<appender name="ConsoleAppender" type="log4net.Appender.ConsoleAppender,log4net">

  <filter type="log4net.Filter.PropertyFilter">
    <key value="ApplicationMode" />
    <stringToMatch value="Console" />
    <acceptOnMatch value="true" />
  </filter>
  <filter type="log4net.Filter.DenyAllFilter" />

  <layout type="log4net.Layout.PatternLayout">
    <conversionPattern value="%date %-5level %-25.25logger{1} - %message%newline" />
  </layout>
</appender>

和用于生产的文件附加器

and the file appender for your production

<appender name="FileAppender" type="log4net.Appender.FileAppender,log4net">

  <filter type="log4net.Filter.PropertyFilter">
    <key value="ApplicationMode" />
    <stringToMatch value="Service" />
    <acceptOnMatch value="true" />
  </filter>
  <filter type="log4net.Filter.DenyAllFilter" />

  <file value="mylogfile.txt" />
  <appendToFile value="true" />
  <lockingModel type="log4net.Appender.FileAppender+MinimalLock" />

  <layout type="log4net.Layout.PatternLayout">
    <conversionPattern value="%date %-5level %-25.25logger{1} - %message%newline" />
  </layout>
</appender>

然后声明日志的所有附加程序:

Then declare all appenders for your logs:

<root>
  <level value="DEBUG"/>
  <appender-ref ref="FileAppender"/>
  <appender-ref ref="ConsoleAppender"/>
</root>

这将根据全局属性中的值将您的消息分派到正确的附加程序. DenyAllFilter很重要; log4net的工作方式是让所有消息默认通过,并且您只希望与PropertyFilter

This will dispatch your message to the correct appender depending on the value that is in the global property. The DenyAllFilter is important; log4net works by letting all messages go through by default, and you only want the messages matching your PropertyFilter

这篇关于基于log4net中的构建配置的不同追加程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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