I have a web service with hundreds of concurrent users, I have a static Logging class to log errors that are thrown. I need to track UserID in the logger which originally wasn't been tracked.
This sound very easy because I could just pass the UserID into the Logger function call, but because this is a legacy system, I CANNOT change the call parameter. I can add some code when the program first start, or inside the logger itself, but to modify every call signature is out of the question.
How do I get userID in the logger without passing to it in every logger call?
Here is my current function and what my problem is:
public static class Log
{
public static void Error(object message, Exception ex)
{
log.Error(message, ex);
IDictionary customData = new Dictionary<string, string>() {{"message", message.ToString()}};
customData.Add("UserID",???????);
DoLogging(ex, customData);
}
}
Here is the trivia solution which I can't use:
public static class Log
{
public static void Error(object message, Exception ex, int userID)
{
log.Error(message, ex);
IDictionary customData = new Dictionary<string, string>() {{"message", message.ToString()}};
customData.Add("UserID", userID.ToString());
DoLogging(ex, customData);
}
}
One potential solution is to save some type of static variable in the Logger class, but remember, this class is static, and there could be hundreds of concurrent instances, how do I tell which instance is calling the logger?
public static class Log
{
private static concurrent DataStrucutre userIDs;
public static void Error(object message, Exception ex)
{
log.Error(message, ex);
IDictionary customData = new Dictionary<string, string>() {{"message", message.ToString()}};
customData.Add("UserID",userIDs[????]);
DoLogging(ex, customData);
}
}