コード例 #1
0
        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));
            }
        }