/* The strategy for asynchronous logging is to keep the load of generating * the log event to a minimum and to push as much work as is reasonably * possible off onto the dispatcher. This is done by putting the efforts * of building the log event into an action which is queued into the * dispatcher. The only bit that can't be put into the action is building * the stack frame for the log event. This is minimized by having the * framework default to a "non-debug" mode, where the stack frame isn't * needed, making the stack frame "optional". * * In a future release, the stack frame aquizition should be moved into * a separate function and wrapped in a compiler directive, so that * a "portable class library" can be built for the logging framework, * which is a future goal of the framework. */ /// <summary> /// Logs the given log event, hinting to the framework that the event should be handled asynchronously /// </summary> /// <param name="logEventInfo">The log event to log</param> public void Log(LogEvent logEventInfo) { var stackFrame = Dispatcher.LoggingConfig.Debug ? new StackFrame(1) : null; Dispatcher.Log(() => { logEventInfo.LoggingStackFrame = stackFrame; logEventInfo.Tags = GetTags(logEventInfo.Tags, stackFrame, logEventInfo.Exception != null); Dispatcher.Log(logEventInfo); }); }
// Internal function for handing exceptions thrown within the scope of the dispatcher public void HandleException(Exception e, LogEvent logEventInfo = null) { // Format the exception string message = logEventInfo != null ? String.Format(FailedToLogMessage, logEventInfo.Message, e) : String.Format(ExceptionInNuLogMessage, e); // Pass the exception to the exception handler, if we have one, otherwise write it out to trace if (ExceptionHandler != null) ExceptionHandler.Invoke(e, message); else if (logEventInfo == null || logEventInfo.Silent == false) Trace.WriteLine(message); }