开发者

Is there a way to implement rolling log out put using standard .NET trace listener config (as opposed to one massive file)

开发者 https://www.devze.com 2023-01-09 05:01 出处:网络
I\'m using .net web config to create trace listener for debug and trace output in a .NET web app. The problem is that if left, the log file, which always uses the same name can get massive and has ac

I'm using .net web config to create trace listener for debug and trace output in a .NET web app.

The problem is that if left, the log file, which always uses the same name can get massive and has actual开发者_开发知识库ly caused me some application issues today.

I can't find a method on the net of setting a log file size limit, or a method of using a dynamic name, such as one that uses a date string as part of the name.

Does anyone know if this is possible?

So far I am using:

<system.diagnostics>
        <trace autoflush="true" indentsize="4">
            <listeners>
                <add name="CollectionLister" type="System.Diagnostics.TextWriterTraceListener" initializeData="Collections.log" />
            </listeners>
        </trace>
    </system.diagnostics>


Personally, I would use a more sophisticated logging framework, like nlog

That can do the file splitting policy stuff for you.


Like Will, I'd suggest using another logging framework.

My preference is Microsoft Enterprise Libraries's logging application blog.


See Apache log4net

<appender name="RollingFileAppender" type="log4net.Appender.RollingFileAppender">
    <file value="log.txt" />
    <appendToFile value="true" />
    <rollingStyle value="Size" />
    <maxSizeRollBackups value="10" />
    <maximumFileSize value="100KB" />
    <staticLogFileName value="true" />
    <layout type="log4net.Layout.PatternLayout">
        <conversionPattern value="%date [%thread] 
            %-5level %logger [%property{NDC}] - %message%newline" />
    </layout>
</appender>


Codeplex hosts a project that has additional trace listeners and filters that plug directly into the existing .NET Trace system you are using. That should minimise any need to change your code.

This includes a rolling flat file logger.

See essentialdiagnostics for details.

0

精彩评论

暂无评论...
验证码 换一张
取 消