public void Log <TState>(LogLevel logLevel, EventId eventId, TState state, Exception exception, Func <TState, Exception, string> formatter) { if (!IsEnabled(logLevel)) { return; } // See if they want the formatted message if (_eventSource.IsEnabled(EventLevel.Critical, LoggingEventSource.Keywords.FormattedMessage)) { string message = formatter(state, exception); _eventSource.FormattedMessage( logLevel, _factoryID, CategoryName, eventId.ToString(), message); } // See if they want the message as its component parts. if (_eventSource.IsEnabled(EventLevel.Critical, LoggingEventSource.Keywords.Message)) { ExceptionInfo exceptionInfo = GetExceptionInfo(exception); IEnumerable <KeyValuePair <string, string> > arguments = GetProperties(state); _eventSource.Message( logLevel, _factoryID, CategoryName, eventId.ToString(), exceptionInfo, arguments); } // See if they want the json message if (_eventSource.IsEnabled(EventLevel.Critical, LoggingEventSource.Keywords.JsonMessage)) { string exceptionJson = "{}"; if (exception != null) { var exceptionInfo = GetExceptionInfo(exception); var exceptionInfoData = new KeyValuePair <string, string>[] { new KeyValuePair <string, string>("TypeName", exceptionInfo.TypeName), new KeyValuePair <string, string>("Message", exceptionInfo.Message), new KeyValuePair <string, string>("HResult", exceptionInfo.HResult.ToString()), new KeyValuePair <string, string>("VerboseMessage", exceptionInfo.VerboseMessage), }; exceptionJson = ToJson(exceptionInfoData); } IEnumerable <KeyValuePair <string, string> > arguments = GetProperties(state); _eventSource.MessageJson( logLevel, _factoryID, CategoryName, eventId.ToString(), exceptionJson, ToJson(arguments)); } }
private LogLevel GetDefaultLevel() { var allMessageKeywords = LoggingEventSource.Keywords.Message | LoggingEventSource.Keywords.FormattedMessage | LoggingEventSource.Keywords.JsonMessage; if (_eventSource.IsEnabled(EventLevel.Informational, allMessageKeywords)) { if (_eventSource.IsEnabled(EventLevel.Verbose, allMessageKeywords)) { return(LogLevel.Debug); } else { return(LogLevel.Information); } } else { if (_eventSource.IsEnabled(EventLevel.Warning, allMessageKeywords)) { return(LogLevel.Warning); } else { if (_eventSource.IsEnabled(EventLevel.Error, allMessageKeywords)) { return(LogLevel.Error); } else { return(LogLevel.Critical); } } } }