Simple way to perform error logging?

后端 未结 9 1524
佛祖请我去吃肉
佛祖请我去吃肉 2021-01-30 03:52

I\'ve created a small C# winforms application, as an added feature I was considering adding some form of error logging into it. Anyone have any suggestions for good ways to go a

9条回答
  •  庸人自扰
    2021-01-30 04:47

    I wouldn't dig too much on external libraries since your logging needs are simple.

    .NET Framework already ships with this feature in the namespace System.Diagnostics, you could write all the logging you need there by simply calling methods under the Trace class:

    Trace.TraceInformation("Your Information");
    Trace.TraceError("Your Error");
    Trace.TraceWarning("Your Warning");
    

    And then configure all the trace listeners that fit your needs on your app.config file:

    
      // other config
      
        
          
            
            
            
            
          
        
      
      // other config
    
    

    or if you prefer, you can also configure your listeners in your application, without depending on a config file:

    Trace.Listeners.Add(new TextWriterTraceListener("MyTextFile.log"));
    

    Remember to set the Trace.AutoFlush property to true, for the Text log to work properly.

提交回复
热议问题