Click here to Skip to main content
Click here to Skip to main content

A Rolling XmlWriterTraceListener

By , 15 Nov 2008
 

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

 
Hint: For improved responsiveness ensure Javascript is enabled and choose 'Normal' from the Layout dropdown and hit 'Update'.
You must Sign In to use this message board.
Search this forum  
    Spacing  Noise  Layout  Per page   
QuestionFails if directory does not existmemberDarkWalkerRzn29 Apr '13 - 22:47 
QuestionProduction environmentmemberMember 99630573 Apr '13 - 7:55 
QuestionAttributes are emptymemberpkoeppe13 Nov '12 - 19:52 
BugRe: Attributes are emptymemberMember 854221628 Jan '13 - 0:44 
QuestionLog is getting cleared everytime the service gets cycled.memberArchie collins10 Jul '12 - 9:02 
SuggestionFix for improper termination of XML tags in trace filesmemberUllas Chacko8 Jul '12 - 15:19 
QuestionNot working :(memberKenny Woo3 Jul '12 - 20:56 
AnswerRe: Not working :(membermarc_opus3 Jul '12 - 21:06 
GeneralRe: Not working :(memberKenny Woo6 Jul '12 - 0:05 
GeneralRe: Not working :(memberoren.shnitzer9 Oct '12 - 21:59 
GeneralMy vote of 5memberrajeevtechnical3 Oct '11 - 4:17 
QuestionWhy the second constructor does not set the current prefix?memberPhilGood19 Jul '11 - 4:37 
AnswerRe: Why the second constructor does not set the current prefix?memberpkkalra197830 Jan '12 - 15:08 
Generalclient crashed when using RollingXmlWriterTraceListenermemberMember 465801610 Sep '10 - 14:21 
GeneralRe: client crashed when using RollingXmlWriterTraceListenermembermarc_opus10 Sep '10 - 20:19 
GeneralRe: client crashed when using RollingXmlWriterTraceListenermemberMember 465801613 Sep '10 - 6:46 
GeneralRe: client crashed when using RollingXmlWriterTraceListenermembermidix18 Mar '13 - 1:33 
GeneralClosing and Disposing streamsmemberGastonV5 Jan '10 - 2:45 
GeneralRe: Closing and Disposing streamsmemberMember 311913114 Jun '11 - 11:49 
GeneralRe: Closing and Disposing streams [modified]memberrajeevtechnical20 Oct '11 - 5:31 
QuestionTest app?memberccfoo24215 Jan '09 - 4:53 

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

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