L A

L A

  • NA
  • 170
  • 168.4k

Trace, Log to file

Jul 7 2018 4:07 PM
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
  1. <system.diagnostics>    
  2.   <trace autoflush="false" indentsize="4"></trace> // what's this for?  
  3.   <sources>    
  4.     <source name ="WebAppLog">    
  5.       <listeners>    
  6.         <add name="FileLog" type="System.Diagnostics.TextWriterTraceListener" initializeData="PartialView_WebApp.log" traceOutputOptions="DateTime,ThreadId,ProcessId,Timestamp,LogicalOperationStack,Callstack">    
  7.           <filter initializeData="All" type="System.Diagnostics.EventTypeFilter"/>    
  8.         </add>    
  9.         <remove name="Default"/>    
  10.       </listeners>    
  11.     </source>    
  12.   </sources>    
  13. </system.diagnostics>  
Added Log.cs to application to log mesages to file.
  1. public class Log  
  2. {  
  3.     static TraceSource source = new TraceSource("WebAppLog"); 
  4.     public static void Message(TraceEventType traceEventType, string message)  
  5.     {  
  6.         short id;  
  7.         switch (traceEventType)  
  8.         {  
  9.             case TraceEventType.Information:  
  10.                 id = 3;  
  11.                 break;  
  12.             case TraceEventType.Verbose:  
  13.                 id = 4;  
  14.                 break;  
  15.             default:  
  16.                 id = -1;  
  17.                 break;  
  18.         }  
  19.         source.TraceEvent(traceEventType, id, message);  
  20.         source.Flush();    
  21.     }
  22. }  
Home controller.cs
  1. public ActionResult Index()  
  2. {  
  3.     try  
  4.     {  
  5.         Log.Message(System.Diagnostics.TraceEventType.Information, "Index Action Start");
  6.         // Do work
  7.         Log.Message(System.Diagnostics.TraceEventType.Information, "Index Action End");  
  8.         return View();  
  9.     }  
  10.     catch (Exception ex)  
  11.     {
  12.         throw;  
  13.     }  
  14. }  
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.?

Answers (2)