简体   繁体   中英

Using log4net in c# without xml files

I want to use log4net for my windows service application to write logs but without using 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.

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.

You can configure log4net to use whatever type of appender you like, not just XML . See here for many different kinds of appenders.

You can configure it in your Web.config (or App.config ) like this, for example:

<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>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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