In my web api whenever any exception occurs i need to log the exception details into the database, along with the exception message and stack trace i need to store ClientId (The client application Id) and ClientUserId(The client user id) which will be available in all the action methods inside my web api.
Whenever any exception occurs the OnException method in my CustomExceptionFilter class automatically gets called, how do i pass the "ClientId", "ClientUserId" to this method when any exception occurs ?
public class CustomExceptionFilter: ExceptionFilterAttribute
{
public override void OnException(HttpActionExecutedContext actionExecutedContext)
{
if(actionExecutedContext.Exception != null)
{
ExceptionLogger.LogException("ClientId", "ClientUserId",actionExecutedContext.Exception.Message, actionExecutedContext.Exception.StackTrace);
}
}
}
Registered the filter in webapi config class
public static class WebApiConfig
{
public static void Register(HttpConfigurationconfig)
{
config.Filters.Add(new CustomExceptionFilter());
}
}
Logger.LogException("Error: {message} \n ClientId: {ClientId}; ClientUserId: {ClientUserId}", actionExecutedContext.Exception.Message, ClientId, ClientUserId);The parameters are saved in the log message in a structured way. Did not try it in MSLogging myself though