Simple way to perform error logging?

后端 未结 9 1488
佛祖请我去吃肉
佛祖请我去吃肉 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:46

    An optimal solution, in my opinion, would be to use NLog: http://nlog-project.org/

    Just install the config package from NuGet: http://www.nuget.org/packages/NLog.Config/ and you will end up with the library and a pre-configured file logger...

    Then in your code you just need:

    // A logger member field:
    
    private readonly Logger logger = LogManager.GetCurrentClassLogger(); // creates a logger using the class name
    
    // use it:
    logger.Info(...);
    logger.Error(...);
    
    // and also:
    logger.ErrorException("text", ex); // which will log the stack trace.
    

    In the config file you get, you need to uncomment the sections that you need:

    
    
        
        
            
    
            
        
    
        
            
    
            
        
    
    

    Edit the properties of the nlog.config file to

    Copy to Output Directory: Copy always
    

提交回复
热议问题