public void DoSomething()
{
try
{
// do whatever you need to do
}
catch (BusinessException ex)
{
// log this with a lower severity
// create the message to be logged
// concat the stack trace or whatever good way to document the error
// call your logging tool of choice /SemanticLogging, Log4Net, NLog, ...)
}
catch (TechnicalException ex)
{
// log this with a higher severity
// create the message to be logged
// concat the stack trace or whatever good way to document the error
// call your logging tool of choice /SemanticLogging, Log4Net, NLog, ...)
}
catch (Exception ex)
{
// make all hell brake loose!
// create the message to be logged
// concat the stack trace or whatever good way to document the error
// call your logging tool of choice /SemanticLogging, Log4Net, NLog, ...)
}
}
public void DoSomething()
{
// do whatever you need to do
}
[MyExceptionHandler]
public void DoSomething()
{
// do whatever you need to do
}
[MyExceptionHandler]
public class MyClass
{
public void DoSomething()
{
// do whatever you need to do
}
}
[Serializable]
public class MyExceptionHandlerAttribute : OnExceptionAspect
{
public MyExceptionHandlerAttribute (){ }
public override void OnException(MethodExecutionArgs args)
{
if (args.Exception.GetType().Equals(typeof(BusinessException)))
{
if (args.Exception.InnerException == null)
MyEventSource.Log.BusinessError(args.Exception.Message);
else
{
var message = args.Exception.Message;
var extraInfo = args.Exception.InnerException.ToString();
MyEventSource.Log.BusinessError(message, extraInfo);
}
}
else if (args.Exception.GetType().Equals(typeof(TechnicalException)))
{
var message = args.Exception.Message;
var extraInfo = args.Exception.InnerException?.ToString();
var stackTrace = args.Exception.StackTrace;
MyEventSource.Log.TechnicalError(message, extraInfo, stackTrace);
}
else
{
var message = args.Exception.Message;
var extraInfo = args.Exception.InnerException?.Message;
MyEventSource.Log.UnhandledError(message, extraInfo);
}
/*
USE THIS IF YOU DON'T WANT TO RETHROW THE EXCEPTION.
LIKE THIS YOU LOG THE ERROR AND THE APPLICATION WON'T COMPLAIN.
IF THIS IS FOR A WEB APP, YOU CAN CONSIDER NOTIFYING THE USER OR
REDIRECTING TO AN ERROR PAGE */
args.FlowBehavior = FlowBehavior.Continue;
}
}
public override void OnException(MethodExecutionArgs args)
{
// handle the exception
}
The single argument, MethodExecutionArgs, has all the information you need in order to decide what to do with the exception. Have a look at the documentation for a complete reference.
[EventSource(Name = "MyApp")]
public class MyEventSource : EventSource
{
public class Keywords
{
public const EventKeywords Page = (EventKeywords)1;
public const EventKeywords DataBase = (EventKeywords)2;
public const EventKeywords Diagnostic = (EventKeywords)4;
public const EventKeywords Perf = (EventKeywords)8;
}
public class Tasks
{
public const EventTask Page = (EventTask)1;
public const EventTask DBQuery = (EventTask)2;
}
private static MyEventSource _log = new MyEventSource();
private MyEventSource() { }
public static MyEventSource Log { get { return _log; } }
[Event(500, Message = "Unhandled Error: {0}. Extra Info: {1}", Level = EventLevel.Critical, Keywords = Keywords.Diagnostic)]
internal void UnhandledError(string message, string extraInfo)
{
if (this.IsEnabled())
this.WriteEvent(500, message ?? "", extraInfo ?? "");
}
[Event(501, Message = "Application Technical Error: {0}. Extra Info: {1}. StackTrace: {2}", Level = EventLevel.Critical, Keywords = Keywords.Diagnostic)]
internal void TechnicalError(string message, string extraInfo, string stackTrace)
{
if (this.IsEnabled())
this.WriteEvent(501, message ?? "", extraInfo ?? "", stackTrace ?? "");
}
[Event(400, Message = "Application Business Error: {0}. Extra Info: {1}", Level = EventLevel.Critical, Keywords = Keywords.Diagnostic)]
internal void BusinessError(string message, string extraInfo = null)
{
if (this.IsEnabled())
this.WriteEvent(400, message ?? "", extraInfo ?? "");
}
[Event(200, Message = "{0}", Level = EventLevel.Informational)]
public void Information(string message)
{
if (this.IsEnabled()) this.WriteEvent(200, message ?? "");
}
}
Have a question about something in this article? You can receive help directly from the article author. Sign up for a free trial to get started.
Comments (0)