log4net每次运行一个文件 [英] log4net one file per run

查看:289
本文介绍了log4net每次运行一个文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要我的应用程序在每次运行时创建一个日志文件.

I need my application to create a log file each time it runs.

我的首选格式为 App.log.yyyy-MM-dd_HH-mm-ss .如果不可能,我会选择 App.log.yyyy-MM-dd.counter

My preferred format would be App.log.yyyy-MM-dd_HH-mm-ss. If that's not possible, I'd settle for App.log.yyyy-MM-dd.counter

这是我当前的追加程序配置:

This is my current appender configuration:

<appender name="File" type="log4net.Appender.RollingFileAppender">
  <file value="App.log"/>
  <rollingStyle value="Date"/>
  <datePattern value=".yyyy-MM-dd_HH-mm-ss"/>
  <staticLogFileName value="false"/>
  <lockingModel type="log4net.Appender.FileAppender+MinimalLock" />
</appender>

但是它会根据日期和时间创建随机数量的文件.

But it creates a random number of files based on the date and time.

推荐答案

我假设应用程序每次运行时都只创建一个日志文件,因此您不需要滚动文件附加器(尽管我的解决方案适用于滚动文件追加程序):

I assume that the application should create only one log file every time it runs, so you do not need a rolling file appender (though my solution would apply for rolling file appenders as well):

<appender name="FileAppender" type="log4net.Appender.FileAppender">
    <file type="log4net.Util.PatternString" value="c:\temp\App-%date{yyyy-MM-dd_HH-mm-ss}.log" />
    <appendToFile value="true" />
    <layout type="log4net.Layout.PatternLayout">
        <conversionPattern value="%date [%2thread] %-5level - %message%newline" />
    </layout>
</appender>

(显然,您可以对文件附加程序使用其他自己的布局和其他设置.)

(Obviously you can use other your own layout and other settings for the file appender.)

这篇关于log4net每次运行一个文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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