Add Timestamp to Trace.WriteLine()

前端 未结 6 966
甜味超标
甜味超标 2021-01-03 20:37

In my C# .NET application I have an issue with the Trace.WriteLine()-method. I uses this method alot, and want to add a TimeStamp every time I use it.

Instead of Tra

6条回答
  •  时光取名叫无心
    2021-01-03 21:32

    Via code

    You can configure the TraceOutputOptions flags enum.

    var listener = new ConsoleTraceListener() { TraceOutputOptions = TraceOptions.Timestamp | TraceOptions.Callstack };
    Trace.Listeners.Add(listener);
    
    Trace.TraceInformation("hello world");
    

    This does not work for Write and WriteLine, you have use the TraceXXX methods.

    Via app.config

    This can also be configured in your App.config with a somewhat equivalent and using TraceSource:

    
      
        
          
            
              
                
              
            
          
        
      
    
    

    And in code you can:

    private static TraceSource mySource =   
            new TraceSource("TraceSourceApp");
    static void Main(string[] args)
    {
      mySource.TraceInformation("hello world");
    }
    

提交回复
热议问题