Simple way to perform error logging?

2019-03-08 03:50发布

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 about this? This is a feature I've never looked into adding to previous projects, so I'm open to suggestions from Developers who have more experience.

I was considering something along the lines of writing exceptions to a specified text file, or possibly a database table. This is an application that will be in use for a few months and then discarded when a larger product is finished.

8条回答
小情绪 Triste *
2楼-- · 2019-03-08 04:11

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:

    <!-- language: xml -->
    <?xml version="1.0" encoding="utf-8" ?> 
    <configuration> 
        <configSections> 
            <section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler, log4net" /> 
        </configSections> 
            <startup> 
                    <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5" /> 
            </startup> 
        <log4net debug="false"> 
            <appender name="LogFileAppender" type="log4net.Appender.FileAppender,log4net" > 
                <param name="File" value="myLog.log" /> 
                <param name="AppendToFile" value="true" /> 
                <layout type="log4net.Layout.PatternLayout,log4net"> 
                    <param name="ConversionPattern" value="%date [%thread] %-5level %logger %ndc - %message%newline" /> 
                </layout>      
            </appender> 
            <root> 
                <priority value="ALL" /> 
                <appender-ref ref="LogFileAppender" /> 
            </root> 
            <category name="testApp.LoggingExample"> 
                <priority value="ALL" /> 
            </category> 
        </log4net> 
    </configuration>
    

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
查看更多
地球回转人心会变
3楼-- · 2019-03-08 04:15

You just write out your exception errors to a text file. Write to Text File. One suggestion is to put the file you create in a userdata or appdata directory though, so you do not have to struggle with permissions.

Since this is only needed for a few months and will be discarded there is no reason to go overboard with DB. A simple text file should suffice.

查看更多
登录 后发表回答