简体   繁体   English

在没有XML文件的C#中使用log4net

[英]Using log4net in c# without xml files

I want to use log4net for my windows service application to write logs but without using xml. 我想为Windows服务应用程序使用log4net来写日志,但不使用xml。 A piece of code would help. 一段代码会有所帮助。 Thank You 谢谢

I think you mean that you do not want an external configuration file for log4net, but you want it embedded in your app.config or web.config. 我认为您的意思是您不需要log4net的外部配置文件,但希望将其嵌入到app.config或web.config中。

Configuring it from code is not a good idea. 从代码配置它不是一个好主意。 You often need to maintain that configuration, change file location, change log size, etc. So you need an easily accessible location to do it. 您通常需要维护该配置,更改文件位置,更改日志大小等。因此,您需要一个易于访问的位置来执行此操作。

Here's an article that shows you how to embed the log4net configuration in your application configuration file. 是一篇文章,向您展示如何在应用程序配置文件中嵌入log4net配置。

You can configure log4net to use whatever type of appender you like, not just XML . 您可以将log4net配置为使用所需的任何类型的附加程序,而不仅仅是XML See here for many different kinds of appenders. 看到这里有许多不同种类的附加器。

You can configure it in your Web.config (or App.config ) like this, for example: 您可以像这样在Web.config (或App.config )中配置它,例如:

<log4net>  
    <appender name="LogFileAppender" type="log4net.Appender.FileAppender, log4net">
      <param name="File" value="Log\Log.NHibernate.txt" />
      <param name="AppendToFile" value="false" />
      <layout type="log4net.Layout.PatternLayout, log4net">
        <param name="ConversionPattern" value="%m%n" />
      </layout>
    </appender>
    <!-- Note: Priority level can be ALL/DEBUG/INFO/WARN/ERROR/FATAL/OFF -->
    <root>
      <priority value="DEBUG" />
      <appender-ref ref="LogFileAppender" />
    </root>
  </log4net>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM