Question

Here is my logging signature

public static void Debug(string message,Exception exception = null,object model = null,object userId = null,[CallerMemberName] string memberName = "",params object[] formatParams)

and I am trying to use it say like

Logger.Debug("My message: {0} {1}", formatParams:1,2)

How can i do this? I cannot find it on the net, I hope this is possible. I tried an anon type but didnt work.

Logger.Debug("My message: {0} {1}", formatParams: new{1,2})

Resolution: I ended up using the example below. I was getting different errors with any other syntax so wanted to make sure this was saved here.

Logger.Debug("My message: {0} {1} {2}", formatParams: new object[] { 1, "string", 123.12m });

Edit: My issue I was having ended up being related to a dynamic being passed in the formatParams, I fixed this by casting it to the type it actually was before logging it.

Was it helpful?

Solution

Yes, you can. You just have to call it with an array like this:

Logger.Debug("My message: {0} {1}", formatParams: new object[] { 1, 2 });

For a single parameter, this isn't necessary, you can just use:

Logger.Debug("My message: {0}", formatParams: 1);

Note that in many cases, you can also use an implicitly typed array:

Logger.Debug("My message: {0} {1}", formatParams: new[] { 1, 2 });

However, this will pass an array of objects which contains a single array of integers. The parameter value would look actually like this:

new object[] { new int[] { 1, 2 } }
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top