Skip to content

Instantly share code, notes, and snippets.

@sirkirby
Last active August 29, 2015 14:02
Show Gist options
  • Save sirkirby/b304e27da2c3f306ae4b to your computer and use it in GitHub Desktop.
Save sirkirby/b304e27da2c3f306ae4b to your computer and use it in GitHub Desktop.
protected virtual MessageContext MessageFromTraceEvent(TraceEventCache eventCache, string source, TraceEventType eventType, int id, string message, params object[] args)
{
var context = new MessageContext(new Exception(message), new List<string>(), new Dictionary<object, object>());
if (args != null)
{
var localArgs = args.ToList();
// check the args for custom data
var custom = localArgs.FirstOrDefault(a => a is IDictionary);
if (custom != null)
{
context.Data = (IDictionary) custom;
localArgs.Remove(custom);
}
// check the args for tags
var tags = localArgs.FirstOrDefault(a => a is IList<string>);
if (tags != null)
{
context.Tags.AddRange((IList<string>) tags);
localArgs.Remove(tags);
}
// check the args for a custom exception
var error = localArgs.FirstOrDefault(a => a is Exception);
if (error != null)
{
context.Exception = new Exception(message, (Exception) error);
localArgs.Remove(error);
}
// add the rest
var count = 0;
foreach (var leftover in localArgs)
context.Data.Add(String.Format("arg-{0}", count++), leftover);
}
return context;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment