Error logging in C#

前端 未结 15 1784
一个人的身影
一个人的身影 2020-12-04 06:11

I am making my switch from coding in C++ to C#. I need to replace my C++ error logging/reporting macro system with something similar in C#.

In my C++ source I can w

相关标签:
15条回答
  • 2020-12-04 06:30

    ExceptionLess is one of the easiest nuget package available to use for logging. Its an open source project. It automatically takes care of unhandled exception, and options for manually logs are available. You can log to online or self host on local server.

    0 讨论(0)
  • 2020-12-04 06:31

    The built in tracing in System.Diagnostics is fine in the .NET Framework and I use it on many applications. However, one of the primary reasons I still use log4net is that the built in .NET Framework tracing lacks many of the useful full featured appenders that log4net already supplies built in.

    For instance there really isn't a good rolling file trace listener defined in the .NET Framework other than the one in a VB.NET dll which really is not all that full featured.

    Depending on your development environment I would recommend using log4net unless 3rd party tools are not available, then I'd say use the System.Diagnostics tracing classes. If you really need a better appender/tracelistener you can always implement it yourself.

    For instance many of our customers require that we do not use open source libraries when installed on their corporate machines, so in that case the .NET Framework tracing classes are a perfect fit.

    Additionally - http://www.postsharp.org/ is an AOP library I'm looking into that may also assist in logging as demonstrated here on code project:http://www.codeproject.com/KB/dotnet/log4postsharp-intro.aspx.

    0 讨论(0)
  • 2020-12-04 06:36

    Even though I personally hate it, log4net seems to be the de facto standard for C# logging. Sample usage:

    log4net.ILog log = log4net.LogManager.GetLogger(typeof(Program));
    log.Error(“Some error”);
    log.ErrorFormat("Error with inputs {0} and {1}", stringvar, intvar);
    
    0 讨论(0)
  • 2020-12-04 06:36

    Further to the couple of comments realting to the use of the System.Diagnostics methods for logging, I would also like to point out that the DebugView tool is very neat for checking debug output when needed - unless you require it, there is no need for the apps to produce a log file, you just launch DebugView as and when needed.

    0 讨论(0)
  • 2020-12-04 06:38

    Serilog is late to the party here, but brings some interesting options to the table. It looks much like classical text-based loggers to use:

    Log.Information("Hello, {0}", username);
    

    But, unlike earlier frameworks, it only renders the message and arguments into a string when writing text, e.g. to a file or the console.

    The idea is that if you're using a 'NoSQL'-style data store for logs, you can record events like:

    {
        Timestamp: "2014-02-....",
        Message: "Hello, nblumhardt",
        Properties:
        {
            "0": "nblumhardt"
        }
    }
    

    The .NET format string syntax is extended so you can write the above example as:

    Log.Information("Hello, {Name}", username);
    

    In this case the property will be called Name (rather than 0), making querying and correlation easier.

    There are already a few good options for storage. MongoDB and Azure Table Storage seem to be quite popular for DIY. I originally built Serilog (though it is a community project) and I'm now working on a product called Seq, which provides storage and querying of these kinds of structured log events.

    0 讨论(0)
  • 2020-12-04 06:40

    Ditto for log4net. I'm adding my two bits because for actual use, it makes sense to look at some open source implementations to see real world code samples with some handy additions. For log4net, I'd suggest off the top of my head looking at subtext. Particularly take a look at the application start and assemblyinfo bits.

    0 讨论(0)
提交回复
热议问题