Hi, I'm working on ASP.Net MVC web app, using System.Diagnostics.TraceSource to trace and log to file. Added following to web.config
- <system.diagnostics>
- <trace autoflush="false" indentsize="4"></trace> // what's this for?
- <sources>
- <source name ="WebAppLog">
- <listeners>
- <add name="FileLog" type="System.Diagnostics.TextWriterTraceListener" initializeData="PartialView_WebApp.log" traceOutputOptions="DateTime,ThreadId,ProcessId,Timestamp,LogicalOperationStack,Callstack">
- <filter initializeData="All" type="System.Diagnostics.EventTypeFilter"/>
- </add>
- <remove name="Default"/>
- </listeners>
- </source>
- </sources>
- </system.diagnostics>
Added Log.cs to application to log mesages to file.
- public class Log
- {
- static TraceSource source = new TraceSource("WebAppLog");
- public static void Message(TraceEventType traceEventType, string message)
- {
- short id;
- switch (traceEventType)
- {
- case TraceEventType.Information:
- id = 3;
- break;
- case TraceEventType.Verbose:
- id = 4;
- break;
- default:
- id = -1;
- break;
- }
- source.TraceEvent(traceEventType, id, message);
- source.Flush();
- }
- }
Home controller.cs
- public ActionResult Index()
- {
- try
- {
- Log.Message(System.Diagnostics.TraceEventType.Information, "Index Action Start");
- // Do work
- Log.Message(System.Diagnostics.TraceEventType.Information, "Index Action End");
- return View();
- }
- catch (Exception ex)
- {
- throw;
- }
- }
After executing, i'm able to generate log file but couldn't write anything, always the file size is 0 bytes. What could be the possible mistake.?