Simple way to perform error logging?

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

    Heres example for log4net:

    1. Create a new console project called Log4NetTest
    2. Add log4net [1.2.13] nuget package into project
    3. Write following program:

      using System.Threading.Tasks;
      using log4net;
      using System.Text;
      using System.CollectionsGeneric;
      using System;
      namespace Log4NetTest
      {
          class Program
          {
      
              private static readonly ILog _logger = LogManager.GetLogger("testApp.LoggingExample");
      
              static void Main(string[] args)
              {
                  // Configure from App.config. This is marked as obsolete so you can also add config into separate config file
                  // and use log4net.Config.XmlConfigurator method to configure from xml file.            
                  log4net.Config.DOMConfigurator.Configure(); 
      
                  _logger.Debug("Shows only at debug");
                  _logger.Warn("Shows only at warn");
                  _logger.Error("Shows only at error");
      
                  Console.ReadKey();
              }
          }
      }
      
    4. Change your app.config to following:

      
       
       
           
              

    5.Run application and you should find following file from bin\Debug folder:

    2013-12-13 13:27:27,252 [8] DEBUG testApp.LoggingExample (null) - Shows only at debug
    2013-12-13 13:27:27,280 [8] WARN  testApp.LoggingExample (null) - Shows only at warn
    2013-12-13 13:27:27,282 [8] ERROR testApp.LoggingExample (null) - Shows only at error
    

提交回复
热议问题