开发者

How to easily log a dictionary of arguments and serialize to JSON using Trace in C#

开发者 https://www.devze.com 2023-01-08 03:22 出处:网络
I need a flexible way to log keyvalue pairs of arguments using System.Diagnostics.Trace. Basically, when I need to log something in code I would like to make a method call which passes a message and

I need a flexible way to log keyvalue pairs of arguments using System.Diagnostics.Trace.

Basically, when I need to log something in code I would like to make a method call which passes a message and a dictionary of values. Its obviously not hard but I want to make it clean and if possible enforce the key names which are allowed. This is because I want be able to parse the keypair values out later and construct a log viewer which lets you search and sort by the arguments that have been logged.

One way I've experimented with was using the new .Net 4 optional method parameters.

public static void LogInformation(string message, int? someID = null, string someValue = null, int? anotherID = n开发者_运维问答ull, int? andAnotherID = null)

I could then call it this like:

Logger.LogInformation("themessage", someID:5, andAnotherID:2);

This method would then check every parameter and IF it had a value THEN it would add it to an internally built dictionary using consistent key names. I would then serialize the dictionary to JSON and pass it to the Trace.LogInformation method as the message. But this seems way too complicated to maintain. I would have to keep adding new lines in this method as our code-base grew and new values needed to be recorded.

I've also tried giving up the idea of locking down on key names and just trying to find a simple way to pass keypair values to a log method without having to do this:

LogInformation("themessage", new Dictionary<string,string>(){{"someID","1"},{"andAnotherID","2"}});

I tried going down the anonymous & dynamic type route so that developer could call the method like this:

LogInformation("themessage", new { someID = 1, andAnotherID = 2 });

But you can't use the standard serialization libraries to turn that anonymous or dynamic object into JSON.

Are there any other options or ideas I should investigate? Performance is obviously something I need to consider as well, since this method will be called from all over.


You can actually serialize an anonymous object to JSON using the JavaScriptSerializer

JavaScriptSerializer serializer = new JavaScriptSerializer();
var output = serializer.Serialize(your_anon_object);

see this thread:

How do I serialize a C# anonymous type to a JSON string?

And if you want to enforce certain 'keys', is there any reason you don't just want to use a strongly typed class?

0

精彩评论

暂无评论...
验证码 换一张
取 消