Click here to Skip to main content
Licence LGPL3
First Posted 13 Nov 2008
Views 18,385
Downloads 347
Bookmarked 16 times

A Rolling XmlWriterTraceListener

By marc_opus | 15 Nov 2008
A derivation of XmlWriterTraceListener that manages the size of your trace files automatically

1

2
1 vote, 14.3%
3
2 votes, 28.6%
4
4 votes, 57.1%
5
4.08/5 - 7 votes
μ 4.08, σa 1.43 [?]

Introduction

Ever had the problem of growing svclog files after configuring tracing in a productive WCF environment? Did not want to restart the application just for deleting or moving the trace files?

Then, you will like the RollingXmlWriterTraceListener, which is a specialized XmlWriterTraceListener, and is completely compatible with the WCF tracing facility.

Background

When tracing is enabled in a WCF application, and the default XmlWriterTraceListener is used (the listener that produces Microsoft's XML trace format, which is understood by svctraceviewer.exe), it won't allow you to delete a trace file while the application is running. And so it may happen that you see your productive server filling up its hard drive, and you are not allowed to stop it. You can spend your time in calculating when the whole system will be hanging. (Don't ask, our customer did not agree to a short downtime... even under such circumstances.)

That's why I wrote this derivate: it will start a new trace file (with a numeric, four character suffix) after a specified file size is reached.

Using the Code

Using the special trace listener is quite easy. Put the class into one of your basic libraries, adjust the namespace, and then use it instead of the default XmlWriterTraceListener in the app.config file:

<?xml version="1.0"?>
<configuration>
    <system.diagnostics>

        <sources>
            <source name="System.ServiceModel" switchValue="All">
                <listeners>
                    <add name="System.ServiceModel.XmlTrace.Listener" />
                </listeners>
            </source>

            <source name="System.Runtime.Serialization" switchValue="All">
                <listeners>
                    <add name="System.Runtime.Serialization.XmlTrace.Listener" />
                </listeners>
            </source>
        </sources>

        <sharedListeners>
            <add
                type="MarcW.Tools.Diagnostics.RollingXmlWriterTraceListener, MarcW.Tools"
                name="System.ServiceModel.XmlTrace.Listener"
                traceOutputOptions="None"
                initializeData=".Logs\System.ServiceModel.svclog"
                MaxTraceFileSize="134217728" />
            <add
                type="MarcW.Tools.Diagnostics.RollingXmlWriterTraceListener, MarcW.Tools"
                name="System.Runtime.Serialization.XmlTrace.Listener"
                traceOutputOptions="None"
                initializeData=".Logs\System.Runtime.Serialization.svclog"
                MaxTraceFileSize="134217728" />
        </sharedListeners>

    </system.diagnostics>
</configuration>

Make sure you choose the correct type and specify the assembly in which the tracer can find the type. In addition, you need to set the maximum size of a trace file before a new one is started, with the MaxTraceFileSize attribute. This is optional, the default value is 128 MB.

History

  • 2008-11-13
    • First issue
  • 2008-11-14
    • Updated the source code to conform to StyleCop and completed the XMLDoc in some places
    • No functional changes applied

License

This article, along with any associated source code and files, is licensed under The GNU Lesser General Public License (LGPLv3)

About the Author

marc_opus

Software Developer
OPUS GmbH
Germany Germany

Member
C# Developer for KLUSA

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board. (secure sign-in)
 
Search this forum  
 FAQ
    Noise  Layout  Per page   
  Refresh
GeneralMy vote of 5 Pinmemberrajeevtechnical5:17 3 Oct '11  
QuestionWhy the second constructor does not set the current prefix? PinmemberPhilGood5:37 19 Jul '11  
AnswerRe: Why the second constructor does not set the current prefix? Pinmemberpkkalra197816:08 30 Jan '12  
Generalclient crashed when using RollingXmlWriterTraceListener PinmemberMember 465801615:21 10 Sep '10  
GeneralRe: client crashed when using RollingXmlWriterTraceListener Pinmembermarc_opus21:19 10 Sep '10  
GeneralRe: client crashed when using RollingXmlWriterTraceListener PinmemberMember 46580167:46 13 Sep '10  
GeneralClosing and Disposing streams PinmemberGastonV3:45 5 Jan '10  
GeneralRe: Closing and Disposing streams PinmemberMember 311913112:49 14 Jun '11  
GeneralRe: Closing and Disposing streams [modified] Pinmemberrajeevtechnical6:31 20 Oct '11  
QuestionTest app? Pinmemberccfoo2425:53 15 Jan '09  

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Mobile
Web01 | 2.5.120206.1 | Last Updated 15 Nov 2008
Article Copyright 2008 by marc_opus
Everything else Copyright © CodeProject, 1999-2012
Terms of Use
Layout: fixed | fluid