internal void Message(LogLevel Level, int FactoryID, string LoggerName, string EventId, ExceptionInfo Exception, IEnumerable <KeyValuePair <string, string> > Arguments)
 {
     WriteEvent(2, Level, FactoryID, LoggerName, EventId, Exception, Arguments);
 }
Example #2
0
        public void Log <TState>(LogLevel logLevel, EventId eventId, TState state, Exception exception, Func <TState, Exception, string> formatter)
        {
            if (!IsEnabled(logLevel))
            {
                return;
            }
            string message = null;

            // See if they want the formatted message
            if (_eventSource.IsEnabled(EventLevel.Critical, LoggingEventSource.Keywords.FormattedMessage))
            {
                message = formatter(state, exception);
                _eventSource.FormattedMessage(
                    logLevel,
                    _factoryID,
                    CategoryName,
                    eventId.Id,
                    eventId.Name,
                    message);
            }

            // See if they want the message as its component parts.
            if (_eventSource.IsEnabled(EventLevel.Critical, LoggingEventSource.Keywords.Message))
            {
                ExceptionInfo exceptionInfo = GetExceptionInfo(exception);
                IReadOnlyList <KeyValuePair <string, string> > arguments = GetProperties(state);

                _eventSource.Message(
                    logLevel,
                    _factoryID,
                    CategoryName,
                    eventId.Id,
                    eventId.Name,
                    exceptionInfo,
                    arguments);
            }

            // See if they want the json message
            if (_eventSource.IsEnabled(EventLevel.Critical, LoggingEventSource.Keywords.JsonMessage))
            {
                string exceptionJson = "{}";
                if (exception != null)
                {
                    ExceptionInfo exceptionInfo = GetExceptionInfo(exception);
                    KeyValuePair <string, string>[] exceptionInfoData = new[]
                    {
                        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);
                }
                IReadOnlyList <KeyValuePair <string, string> > arguments = GetProperties(state);
                message ??= formatter(state, exception);
                _eventSource.MessageJson(
                    logLevel,
                    _factoryID,
                    CategoryName,
                    eventId.Id,
                    eventId.Name,
                    exceptionJson,
                    ToJson(arguments),
                    message);
            }
        }