Пример #1
0
            public override void Log <TState>(Extensions.Logging.LogLevel logLevel, EventId eventId, TState state, Exception exception, Func <TState, Exception, string> formatter)
            {
                FunctionIndexingException fex = exception as FunctionIndexingException;

                if (fex != null)
                {
                    fex.Handled = true;
                    Errors.Add(fex);
                }

                base.Log(logLevel, eventId, state, exception, formatter);
            }
        public void Log <TState>(Extensions.Logging.LogLevel logLevel, EventId eventId, TState state, Exception exception, Func <TState, Exception, string> formatter)
        {
            Validation.Requires.NotNull(formatter, nameof(formatter));

            if (!IsEnabled(logLevel))
            {
                return;
            }

            var message = formatter(state, exception);

            Dictionary <string, object> properties = new Dictionary <string, object>();

            if (state is FormattedLogValues)
            {
                var formattedState = state as FormattedLogValues;
                //last KV is the whole message, we will pass it separately
                for (int i = 0; i < formattedState.Count - 1; i++)
                {
                    properties.Add(formattedState[i].Key, formattedState[i].Value);
                }
            }

            var scope = EventFlowLoggerScope.Current;

            while (scope != null)
            {
                if (scope?.State != null)
                {
                    var formattedState = scope.State as FormattedLogValues;
                    if (formattedState != null)
                    {
                        for (int i = 0; i < formattedState.Count - 1; i++)
                        {
                            KeyValuePair <string, object> current = formattedState[i];
                            AddPayloadProperty(properties, current.Key, current.Value);
                        }

                        //last KV is the whole 'scope' message, we will add it formatted
                        AddPayloadProperty(properties, "Scope", formattedState.ToString());
                    }
                    else
                    {
                        AddPayloadProperty(properties, "Scope", scope.State);
                    }
                }

                scope = scope.Parent;
            }

            loggerInput.SubmitEventData(message, ToLogLevel(logLevel), eventId, exception, categoryName, properties);
        }
        public void Log <TState>(Extensions.Logging.LogLevel logLevel, EventId eventId, TState state, Exception exception, Func <TState, Exception, string> formatter)
        {
            Validation.Requires.NotNull(formatter, nameof(formatter));

            if (!IsEnabled(logLevel))
            {
                return;
            }

            Dictionary <string, object> properties = new Dictionary <string, object>();

            ApplyOmittingUnformattedMessage(state, item => properties.Add(item.Key, item.Value));

            var scope = EventFlowLoggerScope.Current;

            if (scope != null)
            {
                Stack <string> scopeValueStack;
                if (!this.stackPool.TryTake(out scopeValueStack))
                {
                    scopeValueStack = new Stack <string>();
                }

                while (scope != null)
                {
                    object scopeState = scope.State;
                    if (scopeState != null)
                    {
                        // Scope data may or may not have a formatted message
                        bool unformattedMessageOmitted = ApplyOmittingUnformattedMessage(scopeState, item => AddPayloadProperty(properties, item.Key, item.Value));
                        if (unformattedMessageOmitted)
                        {
                            scopeValueStack.Push(scopeState.ToString());
                        }
                    }

                    scope = scope.Parent;
                }

                if (scopeValueStack.Count > 0)
                {
                    AddPayloadProperty(properties, "Scope", string.Join("\r\n", scopeValueStack));
                }

                scopeValueStack.Clear();
                this.stackPool.Add(scopeValueStack);
            }

            var message = formatter(state, exception);

            loggerInput.SubmitEventData(message, ToLogLevel(logLevel), eventId, exception, categoryName, properties);
        }
        private LogLevel ToLogLevel(Extensions.Logging.LogLevel loggerLevel)
        {
            switch (loggerLevel)
            {
            case Extensions.Logging.LogLevel.Critical:
                return(LogLevel.Critical);

            case Extensions.Logging.LogLevel.Error:
                return(LogLevel.Error);

            case Extensions.Logging.LogLevel.Warning:
                return(LogLevel.Warning);

            case Extensions.Logging.LogLevel.Information:
                return(LogLevel.Informational);
            }
            return(LogLevel.Verbose);
        }
 public bool IsEnabled(Extensions.Logging.LogLevel logLevel)
 {
     return(true);
 }
        public void Log <TState>(Extensions.Logging.LogLevel logLevel, EventId eventId, TState state, Exception exception, Func <TState, Exception, string> formatter)
        {
            Validation.Requires.NotNull(formatter, nameof(formatter));

            if (!IsEnabled(logLevel))
            {
                return;
            }

            var message = formatter(state, exception);

            Dictionary <string, object> properties = new Dictionary <string, object>();

            if (state is IReadOnlyList <KeyValuePair <string, object> > )
            {
                var formattedState = state as IReadOnlyList <KeyValuePair <string, object> >;
                //last KV is the whole message, we will pass it separately
                for (int i = 0; i < formattedState.Count - 1; i++)
                {
                    properties.Add(formattedState[i].Key, formattedState[i].Value);
                }
            }

            var scope = EventFlowLoggerScope.Current;

            if (scope != null)
            {
                Stack <string> scopeValueStack;
                if (!this.stackPool.TryTake(out scopeValueStack))
                {
                    scopeValueStack = new Stack <string>();
                }

                while (scope != null)
                {
                    if (scope.State != null)
                    {
                        var formattedState = scope.State as IReadOnlyList <KeyValuePair <string, object> >;
                        if (formattedState != null)
                        {
                            for (int i = 0; i < formattedState.Count - 1; i++)
                            {
                                KeyValuePair <string, object> current = formattedState[i];
                                AddPayloadProperty(properties, current.Key, current.Value);
                            }

                            //last KV is the whole 'scope' message, we will add it formatted
                            scopeValueStack.Push(formattedState.ToString());
                        }
                        else
                        {
                            scopeValueStack.Push(scope.State.ToString());
                        }
                    }

                    scope = scope.Parent;
                }

                if (scopeValueStack.Count > 0)
                {
                    AddPayloadProperty(properties, "Scope",
                                       string.Join("\r\n", scopeValueStack));
                }

                scopeValueStack.Clear();
                this.stackPool.Add(scopeValueStack);
            }

            loggerInput.SubmitEventData(message, ToLogLevel(logLevel), eventId, exception, categoryName, properties);
        }