I\'m looking for a good TraceListener for .Net that supports rolling over the log file based on size limits.
Constraints
I had the same issue. Secure environment, no open source allowed. Painful. Here's what worked for us.
Derive from TextWriterTraceListener that adds members for max log size, max rolls to keep, and uses a FileStream with Share and Access set to ReadWrite and set OpenOrCreate. It should also create and hold a mutex with a name based on the filename.
Override TraceEvent method, wait for the mutex, seek the stream to end, call Write, check the size and roll if necessary. Release the mutex.
For rolling, rotate the previous rolls via file move and delete, copy the current file to the first level roll name, call SetLength(0) on the stream. All done within the mutex grab, but hopefully not often. This will work across processes and will avoid that crappy {GUID}mylog.log stuff.
I keep this config snippet handy for whenever I need to do the network trace. I don't have to have a project built with explicit reference to VB DLL added as this does by adding the reference in App.config at runtime.
<system.diagnostics>
<sources>
<source name="System.Net">
<listeners>
<add name="System.Net"/>
</listeners>
</source>
<source name="System.Net.Http">
<listeners>
<add name="System.Net"/>
</listeners>
</source>
<source name="System.Net.Sockets">
<listeners>
<add name="System.Net"/>
</listeners>
</source>
</sources>
<switches>
<add name="System.Net" value="Verbose"/>
<add name="System.Net.Http" value="Verbose"/>
<add name="System.Net.Sockets" value="Verbose"/>
</switches>
<sharedListeners>
<add name="System.Net"
type="Microsoft.VisualBasic.Logging.FileLogTraceListener, Microsoft.VisualBasic, Version=10.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a"
traceOutputOptions="DateTime,ProcessId,ThreadId"
customLocation="c:\temp"
location="Custom"
logFileCreationSchedule="Daily"
baseFileName="NetworkTrace"/>
</sharedListeners>
<trace autoflush="true"/>
</system.diagnostics>
And add the reference at runtime
<runtime>
<assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
<dependentAssembly>
<assemblyIdentity name="Microsoft.VisualBasic" culture="neutral" publicKeyToken="b03f5f7f11d50a3a"/>
<codeBase version="10.0.0.0" href="file://C:/Program Files (x86)/Reference Assemblies/Microsoft/Framework/.NETFramework/v4.5/Microsoft.VisualBasic.dll"/>
</dependentAssembly>
</assemblyBinding>
</runtime>
I have used both Log4Net and Nlog. I prefer NLog but really once they are setup you forget they are there anyway (untill something breaks, then your glad it is there!). there should be plenty of documentation on both out in the interweb