Click here to Skip to main content
15,867,568 members
Articles / Programming Languages / C#
Article

A Rolling XmlWriterTraceListener

Rate me:
Please Sign up or sign in to vote.
4.30/5 (14 votes)
15 Nov 2008LGPL31 min read 123.6K   4.2K   24   34
A derivation of XmlWriterTraceListener that manages the size of your trace files automatically

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


Written By
Software Developer OPUS GmbH
Germany Germany
C# Developer for KLUSA

Comments and Discussions

 
QuestionmaxMessagesToLog stops logging Pin
Alexandros Pappas12-Jun-19 22:48
professionalAlexandros Pappas12-Jun-19 22:48 
QuestionNuGet Package Pin
divyesh143223-Oct-18 16:20
divyesh143223-Oct-18 16:20 
QuestionRollingXmlWriterTraceListener in another dll outside the project causing windows service to crash Pin
Tom Hanson12-Nov-17 18:15
Tom Hanson12-Nov-17 18:15 
AnswerRe: RollingXmlWriterTraceListener in another dll outside the project causing windows service to crash Pin
Member 1188808924-Jul-20 5:47
Member 1188808924-Jul-20 5:47 
QuestionAdded System.ServiceModel.MessageLogging Pin
Member 1107155716-Nov-14 23:30
Member 1107155716-Nov-14 23:30 
AnswerRe: Added System.ServiceModel.MessageLogging Pin
Member 1107155717-Nov-14 1:10
Member 1107155717-Nov-14 1:10 
QuestionUpdated Version? Pin
rleonard5525-Sep-14 3:18
rleonard5525-Sep-14 3:18 
AnswerRe: Updated Version? Pin
Member 1107155713-Nov-14 1:17
Member 1107155713-Nov-14 1:17 
QuestionFlush doesn't flush data imediatly to file Pin
ToolMaker200719-Sep-13 4:17
ToolMaker200719-Sep-13 4:17 
QuestionFails if directory does not exist Pin
DarkWalkerRzn29-Apr-13 22:47
DarkWalkerRzn29-Apr-13 22:47 
QuestionProduction environment Pin
Member 99630573-Apr-13 7:55
Member 99630573-Apr-13 7:55 
AnswerRe: Production environment Pin
Johnson.Net15-Nov-13 1:19
Johnson.Net15-Nov-13 1:19 
QuestionAttributes are empty Pin
User 269896713-Nov-12 19:52
User 269896713-Nov-12 19:52 
BugRe: Attributes are empty Pin
Member 854221628-Jan-13 0:44
Member 854221628-Jan-13 0:44 
GeneralRe: Attributes are empty Pin
Johnson.Net15-Nov-13 1:11
Johnson.Net15-Nov-13 1:11 
QuestionLog is getting cleared everytime the service gets cycled. Pin
Archie collins10-Jul-12 9:02
Archie collins10-Jul-12 9:02 
SuggestionFix for improper termination of XML tags in trace files Pin
Ullas Chacko Antony8-Jul-12 15:19
Ullas Chacko Antony8-Jul-12 15:19 
GeneralRe: Fix for improper termination of XML tags in trace files Pin
Member 1250607117-Aug-16 0:11
Member 1250607117-Aug-16 0:11 
QuestionNot working :( Pin
appxdev3-Jul-12 20:56
appxdev3-Jul-12 20:56 
Hi all,
I try to implement this to my project but it didn't work
I added RollingXmlWriterTraceListener.cs file to my project, add some reference, modify these code on Web.config:
XML
<system.diagnostics>
     <sources>     
            <source name="System.ServiceModel" switchValue="All">
                <listeners>
                    <add name="System.ServiceModel.XmlTrace.Listener" />
                </listeners>
            </source>   
        </sources>
    <sharedListeners>        
         <add type="MarcW.Tools.Diagnostics.RollingXmlWriterTraceListener, MarcW.Tools" 
              name="System.ServiceModel.XmlTrace.Listener" 
              traceOutputOptions="None" 
              initializeData="C:\Logs\MyTraceFileName.svclog" 
              MaxTraceFileSize="1048576" />
    </sharedListeners>
  </system.diagnostics>


Let's me know if i missed some thing.
Thanks
AnswerRe: Not working :( Pin
marc_anic3-Jul-12 21:06
marc_anic3-Jul-12 21:06 
GeneralRe: Not working :( Pin
appxdev6-Jul-12 0:05
appxdev6-Jul-12 0:05 
GeneralRe: Not working :( Pin
oren.shnitzer9-Oct-12 21:59
oren.shnitzer9-Oct-12 21:59 
GeneralMy vote of 5 Pin
Geekian_senate3-Oct-11 4:17
Geekian_senate3-Oct-11 4:17 
QuestionWhy the second constructor does not set the current prefix? Pin
PhilGood19-Jul-11 4:37
PhilGood19-Jul-11 4:37 
AnswerRe: Why the second constructor does not set the current prefix? Pin
pkkalra197830-Jan-12 15:08
pkkalra197830-Jan-12 15:08 

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

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