Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What the best rollover log file tracelistener for .NET

Tags:

.net

logging

I'm looking for a good TraceListener for .Net that supports rolling over the log file based on size limits.

Constraints

  • Uses .Net built in Trace logging
  • Independent class or binary that's not part of some gigantic library
  • Allows rolling over a log file based on size
like image 292
user17222 Avatar asked Oct 01 '08 07:10

user17222


4 Answers

You could use Microsoft.VisualBasic.Logging.FileLogTraceListener, which comes built-in with the .NET Framework. Don't let the VisualBasic in the namespace scare you, you'll just have to reference the microsoft.visualbasic.dll assembly and it should work fine with C#.

like image 200
csgero Avatar answered Oct 05 '22 01:10

csgero


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> 
like image 43
Ostati Avatar answered Oct 05 '22 00:10

Ostati


I'm a big fan of log4net (http://logging.apache.org/log4net/index.html), it's very easy to configure and supports just about any log type you want, but can have custom ones written as well.

It can also do different actions depending on the log level. We log all messages to a text file and Error -> Fatal send emails

like image 37
Aaron Powell Avatar answered Oct 05 '22 02:10

Aaron Powell


I am using NLog and I am very satisfied. Source code is well written and it is easy to extend and modify. Documentation is good and it is very easy to configure.

Some Links:

  • Using NLog To Track Events
  • Introduction to NLog
like image 25
Robert Vuković Avatar answered Oct 05 '22 02:10

Robert Vuković