Error logging in C#

前端 未结 15 1910
一个人的身影
一个人的身影 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: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.

提交回复
热议问题