예제 #1
0
        public async Task Should_ignore_exception_from_source()
        {
            var result2 = new SearchResults().Add("Name2", SearchResultType.Setting, "Url2");

            var query = "a query";

            A.CallTo(() => source1.SearchAsync(query, requestContext))
            .Throws(new InvalidOperationException());

            A.CallTo(() => source2.SearchAsync(query, requestContext))
            .Returns(result2);

            var result = await sut.SearchAsync(query, requestContext);

            result.Should().BeEquivalentTo(result2);

            A.CallTo(() => log.Log(A <SemanticLogLevel> ._, A <string> ._, A <Exception?> ._, A <LogFormatter <string> > ._ !))
            .MustHaveHappened();
        }
예제 #2
0
        private static void LogMessage(ISemanticLog log, LogMessage message)
        {
            var level = SemanticLogLevel.Information;

            switch (message.Level)
            {
            case SyslogLevel.Emergency:
                level = SemanticLogLevel.Error;
                break;

            case SyslogLevel.Alert:
                level = SemanticLogLevel.Error;
                break;

            case SyslogLevel.Critical:
                level = SemanticLogLevel.Error;
                break;

            case SyslogLevel.Error:
                level = SemanticLogLevel.Error;
                break;

            case SyslogLevel.Warning:
                level = SemanticLogLevel.Warning;
                break;

            case SyslogLevel.Notice:
                level = SemanticLogLevel.Information;
                break;

            case SyslogLevel.Info:
                level = SemanticLogLevel.Information;
                break;

            case SyslogLevel.Debug:
                level = SemanticLogLevel.Debug;
                break;
            }

            log.Log <None>(level, default, (_, w) => w
예제 #3
0
 public static void LogError(this ISemanticLog log, Action <IObjectWriter> objectWriter)
 {
     log.Log(SemanticLogLevel.Error, None.Value, (_, w) => objectWriter(w));
 }
예제 #4
0
 public static void LogWarning(this ISemanticLog log, Exception exception, LogFormatter action)
 {
     log.Log(SemanticLogLevel.Warning, exception, action);
 }
예제 #5
0
 public static void LogFatal(this ISemanticLog log, Exception exception, Action <IObjectWriter> objectWriter = null)
 {
     log.Log(SemanticLogLevel.Fatal, writer => writer.WriteException(exception, objectWriter));
 }
예제 #6
0
 private void MustLogWarning()
 {
     A.CallTo(() => log.Log(SemanticLogLevel.Warning, A <None> .Ignored, A <Action <None, IObjectWriter> > .Ignored))
     .MustHaveHappened();
 }
        public void Should_generate_400_for_ValidationException()
        {
            var errors = new List <ValidationError>
            {
                new ValidationError("Error1"),
                new ValidationError("Error2", "Property0"),
                new ValidationError("Error3", "Property1", "Property2"),
                new ValidationError("Error4", "Property3.Property4"),
                new ValidationError("Error5", "Property5[0].Property6")
            };

            var ex = new ValidationException(errors);

            var context = Error(ex);

            sut.OnException(context);

            var result = (ObjectResult)context.Result !;

            Assert.Equal(400, result.StatusCode);
            Assert.Equal(400, (result.Value as ErrorDto)?.StatusCode);

            Assert.Equal(new[]
            {
                "Error1",
                "property0: Error2",
                "property1, property2: Error3",
                "property3.property4: Error4",
                "property5[0].property6: Error5"
            }, ((ErrorDto)result.Value).Details);

            A.CallTo(() => log.Log(A <SemanticLogLevel> ._, A <Exception?> ._, A <LogFormatter> ._ !))
            .MustNotHaveHappened();
        }
예제 #8
0
 public static void LogFatal(this ISemanticLog log, Action <IObjectWriter> objectWriter)
 {
     log.Log <None>(SemanticLogLevel.Fatal, null, (_, w) => objectWriter(w));
 }
예제 #9
0
 public static void LogFatal(this ISemanticLog log, Exception exception, Action <IObjectWriter> objectWriter = null)
 {
     log.Log <None>(SemanticLogLevel.Fatal, null, (_, w) => w.WriteException(exception, objectWriter));
 }
예제 #10
0
 public static void LogFatal(this ISemanticLog log, Exception?exception, Action <IObjectWriter>?objectWriter = null)
 {
     log.Log(SemanticLogLevel.Fatal, None.Value, (_, w) => w.WriteException(exception, objectWriter));
 }
예제 #11
0
 public static void LogInformation(this ISemanticLog log, Action <IObjectWriter> objectWriter)
 {
     log.Log <None>(SemanticLogLevel.Information, null, (_, w) => objectWriter(w));
 }
예제 #12
0
 public static void LogFatal <T>(this ISemanticLog log, Exception?exception, T context, LogFormatter <T> action)
 {
     log.Log(SemanticLogLevel.Fatal, context, exception, action);
 }
예제 #13
0
        public void Log <TState>(LogLevel logLevel, EventId eventId, TState state, Exception exception, Func <TState, Exception, string> formatter)
        {
            SemanticLogLevel semanticLogLevel;

            switch (logLevel)
            {
            case LogLevel.Trace:
                semanticLogLevel = SemanticLogLevel.Trace;
                break;

            case LogLevel.Debug:
                semanticLogLevel = SemanticLogLevel.Debug;
                break;

            case LogLevel.Information:
                semanticLogLevel = SemanticLogLevel.Information;
                break;

            case LogLevel.Warning:
                semanticLogLevel = SemanticLogLevel.Warning;
                break;

            case LogLevel.Error:
                semanticLogLevel = SemanticLogLevel.Error;
                break;

            case LogLevel.Critical:
                semanticLogLevel = SemanticLogLevel.Fatal;
                break;

            default:
                semanticLogLevel = SemanticLogLevel.Debug;
                break;
            }

            semanticLog.Log(semanticLogLevel, writer =>
            {
                var message = formatter(state, exception);

                if (!string.IsNullOrWhiteSpace(message))
                {
                    writer.WriteProperty(nameof(message), message);
                }

                if (eventId.Id > 0)
                {
                    writer.WriteObject(nameof(eventId), eventIdWriter =>
                    {
                        eventIdWriter.WriteProperty("id", eventId.Id);

                        if (!string.IsNullOrWhiteSpace(eventId.Name))
                        {
                            eventIdWriter.WriteProperty("name", eventId.Name);
                        }
                    });
                }

                if (state is IReadOnlyList <KeyValuePair <string, object> > parameters)
                {
                    foreach (var kvp in parameters)
                    {
                        if (kvp.Value != null)
                        {
                            var key = kvp.Key.Trim('{', '}', ' ');

                            if (key.Length > 2 && !string.Equals(key, "originalFormat", StringComparison.OrdinalIgnoreCase))
                            {
                                writer.WriteProperty(key.ToCamelCase(), kvp.Value.ToString());
                            }
                        }
                    }
                }

                if (exception != null)
                {
                    writer.WriteException(exception);
                }
            });
        }
예제 #14
0
 public static void LogFatal(this ISemanticLog log, Exception?exception, LogFormatter action)
 {
     log.Log(SemanticLogLevel.Fatal, exception, action);
 }
예제 #15
0
 public static void LogFatal(this ISemanticLog log, LogFormatter action)
 {
     log.Log(SemanticLogLevel.Fatal, null, action);
 }
예제 #16
0
 public static void LogError <T>(this ISemanticLog log, Exception exception, T context, LogFormatter <T> action)
 {
     log.Log(SemanticLogLevel.Error, context, exception, action);
 }
예제 #17
0
 public static void LogError(this ISemanticLog log, Exception exception, LogFormatter action)
 {
     log.Log(SemanticLogLevel.Error, exception, action);
 }
예제 #18
0
 public static void LogError(this ISemanticLog log, LogFormatter action)
 {
     log.Log(SemanticLogLevel.Error, null, action);
 }
예제 #19
0
 public static void LogTrace(this ISemanticLog log, Action <IObjectWriter> objectWriter)
 {
     log.Log(SemanticLogLevel.Trace, objectWriter);
 }
예제 #20
0
 public static void LogDebug(this ISemanticLog log, Action <IObjectWriter> objectWriter)
 {
     log.Log(SemanticLogLevel.Debug, objectWriter);
 }
예제 #21
0
 public static void LogWarning <T>(this ISemanticLog log, T context, Action <T, IObjectWriter> objectWriter)
 {
     log.Log(SemanticLogLevel.Warning, context, objectWriter);
 }
예제 #22
0
 public static void LogInformation(this ISemanticLog log, Action <IObjectWriter> objectWriter)
 {
     log.Log(SemanticLogLevel.Information, objectWriter);
 }
예제 #23
0
 public static void LogFatal <T>(this ISemanticLog log, T context, Action <T, IObjectWriter> objectWriter)
 {
     log.Log(SemanticLogLevel.Fatal, context, objectWriter);
 }
예제 #24
0
 public static void LogWarning(this ISemanticLog log, Action <IObjectWriter> objectWriter)
 {
     log.Log(SemanticLogLevel.Warning, objectWriter);
 }
예제 #25
0
 public static void LogFatal <T>(this ISemanticLog log, Exception exception, T context, Action <T, IObjectWriter> objectWriter = null)
 {
     log.Log(SemanticLogLevel.Fatal, context, (ctx, w) => w.WriteException(exception, ctx, objectWriter));
 }
예제 #26
0
 public static void LogError(this ISemanticLog log, Action <IObjectWriter> objectWriter)
 {
     log.Log(SemanticLogLevel.Error, objectWriter);
 }
예제 #27
0
        public void Log <TState>(LogLevel logLevel, EventId eventId, TState state, Exception exception, Func <TState, Exception, string> formatter)
        {
            SemanticLogLevel semanticLogLevel;

            switch (logLevel)
            {
            case LogLevel.Trace:
                semanticLogLevel = SemanticLogLevel.Trace;
                break;

            case LogLevel.Debug:
                semanticLogLevel = SemanticLogLevel.Debug;
                break;

            case LogLevel.Information:
                semanticLogLevel = SemanticLogLevel.Information;
                break;

            case LogLevel.Warning:
                semanticLogLevel = SemanticLogLevel.Warning;
                break;

            case LogLevel.Error:
                semanticLogLevel = SemanticLogLevel.Error;
                break;

            case LogLevel.Critical:
                semanticLogLevel = SemanticLogLevel.Fatal;
                break;

            default:
                semanticLogLevel = SemanticLogLevel.Debug;
                break;
            }

            if (state is IReadOnlyList <KeyValuePair <string, object> > parameters)
            {
                foreach (var(_, value) in parameters)
                {
                    if (value is Exception ex && exception == null)
                    {
                        exception = ex;
                    }
                }
            }

            var context = (eventId, state, exception, formatter);

            semanticLog.Log(semanticLogLevel, context, exception, (ctx, writer) =>
            {
                var message = ctx.formatter(ctx.state, ctx.exception);

                if (!string.IsNullOrWhiteSpace(message))
                {
                    writer.WriteProperty(nameof(message), message);
                }

                if (ctx.eventId.Id > 0)
                {
                    writer.WriteObject("eventId", ctx.eventId, (innerEventId, eventIdWriter) =>
                    {
                        eventIdWriter.WriteProperty("id", innerEventId.Id);

                        if (!string.IsNullOrWhiteSpace(innerEventId.Name))
                        {
                            eventIdWriter.WriteProperty("name", innerEventId.Name);
                        }
                    });
                }

                if (ctx.state is IReadOnlyList <KeyValuePair <string, object> > parameters2)
                {
                    foreach (var(key, value) in parameters2)
                    {
                        if (value != null)
                        {
                            var trimmedName = key.Trim('{', '}', ' ');

                            if (trimmedName.Length > 2 &&
                                !string.Equals(trimmedName, "exception", StringComparison.OrdinalIgnoreCase) &&
                                !string.Equals(trimmedName, "originalFormat", StringComparison.OrdinalIgnoreCase))
                            {
                                writer.WriteProperty(trimmedName.ToCamelCase(), value.ToString());
                            }
                        }
                    }
                }
예제 #28
0
 public static void LogFatal(this ISemanticLog log, Action <IObjectWriter> objectWriter)
 {
     log.Log(SemanticLogLevel.Fatal, objectWriter);
 }
예제 #29
0
 private void MustLogWarning()
 {
     A.CallTo(() => log.Log(A <SemanticLogLevel> ._, A <Exception?> ._, A <LogFormatter> ._ !))
     .MustHaveHappened();
 }
예제 #30
0
 public static void LogWarning <T>(this ISemanticLog log, T context, LogFormatter <T> action)
 {
     log.Log(SemanticLogLevel.Warning, context, null, action);
 }