2

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 = null, 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.

1

1 Answer 1

1

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?

Sign up to request clarification or add additional context in comments.

2 Comments

Because the list off possible values to log will keep growing and growing. Also, I didn't want to use JavaScriptSerializer because I thought it was deprecated. Looks like it isn't anymore.
OK. However, if you don't have to define the 'keys' runtime, you'll have to define them somewhere compile time, and I don't see the problem with a class with a lot of properties. (some people do though, matter of taste I guess :))

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.