Exemplo n.º 1
0
        public void Append(IObjectWriter writer)
        {
            var actionContext = actionContextAccessor.ActionContext;

            if (actionContext == null)
            {
                return;
            }

            var httpContext = actionContext.HttpContext;

            Guid requestId;

            if (httpContext.Items.TryGetValue(nameof(requestId), out var value) && value is Guid requestIdValue)
            {
                requestId = requestIdValue;
            }
            else
            {
                httpContext.Items[nameof(requestId)] = requestId = Guid.NewGuid();
            }

            writer.WriteObject("web", w => w
                               .WriteProperty("requestId", requestId.ToString())
                               .WriteProperty("requestPath", httpContext.Request.Path)
                               .WriteProperty("requestMethod", httpContext.Request.Method)
                               .WriteObject("routeValues", r =>
            {
                foreach (var kvp in actionContext.ActionDescriptor.RouteValues)
                {
                    r.WriteProperty(kvp.Key, kvp.Value);
                }
            }));
        }
Exemplo n.º 2
0
 public void Append(IObjectWriter writer, SemanticLogLevel logLevel)
 {
     writer.WriteObject("app", w => w
                        .WriteProperty("name", applicationName)
                        .WriteProperty("version", applicationVersion)
                        .WriteProperty("sessionId", applicationSessionId));
 }
Exemplo n.º 3
0
        public void Append(IObjectWriter writer, SemanticLogLevel logLevel, Exception?exception)
        {
            var httpContext = httpContextAccessor.HttpContext;

            if (string.IsNullOrEmpty(httpContext?.Request?.Method))
            {
                return;
            }

            var requestId = GetRequestId(httpContext);

            var logContext = (requestId, context : httpContext, actionContextAccessor);

            writer.WriteObject("web", logContext, (ctx, w) =>
            {
                w.WriteProperty("requestId", ctx.requestId);
                w.WriteProperty("requestPath", ctx.context.Request.Path);
                w.WriteProperty("requestMethod", ctx.context.Request.Method);

                var actionContext = ctx.actionContextAccessor.ActionContext;

                if (actionContext != null)
                {
                    w.WriteObject("routeValues", actionContext.ActionDescriptor.RouteValues, (routeValues, r) =>
                    {
                        foreach (var(key, value) in routeValues)
                        {
                            r.WriteProperty(key, value);
                        }
                    });
Exemplo n.º 4
0
 public static IObjectWriter WriteMethodInfo(this IObjectWriter writer, string property = "caller", [CallerFilePath] string file = "", [CallerLineNumber] int line = 0, [CallerMemberName] string member = "")
 {
     return(writer.WriteObject(property, x =>
                               x.WriteProperty("file", file)
                               .WriteProperty("line", line)
                               .WriteProperty("member", member)));
 }
Exemplo n.º 5
0
 public static IObjectWriter WriteException(this IObjectWriter writer, Exception exception)
 {
     return(writer.WriteObject(nameof(exception), inner =>
     {
         inner.WriteProperty("message", exception.Message);
         inner.WriteProperty("stackTrace", exception.StackTrace);
     }));
 }
Exemplo n.º 6
0
 public static IObjectWriter WriteException(this IObjectWriter writer, Exception exception)
 {
     return(writer.WriteObject(nameof(exception), exception, (ctx, w) =>
     {
         w.WriteProperty("type", ctx.GetType().FullName);
         w.WriteProperty("message", ctx.Message);
         w.WriteProperty("stackTrace", ctx.StackTrace);
     }));
 }
Exemplo n.º 7
0
    public static IObjectWriter WriteException(this IObjectWriter writer, Exception?exception, string property = "exception")
    {
        if (exception == null)
        {
            return(writer);
        }

        return(writer.WriteObject(property, x => InternalWriteException(x, exception)));
    }
 public static void DumpRequestHeader(HttpContext httpContext, IObjectWriter logEntry)
 {
     logEntry.WriteObject("Headers", x =>
     {
         foreach (KeyValuePair <string, StringValues> value in httpContext.Request.Headers)
         {
             x.WriteProperty(value.Key, value.Value.ToString());
         }
     });
 }
Exemplo n.º 9
0
        public void Write(IObjectWriter writer)
        {
            Guard.NotNull(writer);

            if (traces.Count > 0)
            {
                writer.WriteObject("profiler", p =>
                {
                    foreach (var(key, profilerItem) in traces)
                    {
                        p.WriteObject(key, profilerItem, (value, k) => k
                                      .WriteProperty("elapsedMsTotal", value.Total)
                                      .WriteProperty("elapsedMsAvg", value.Total / value.Count)
                                      .WriteProperty("count", value.Count));
                    }
                });
Exemplo n.º 10
0
        public void Write(IObjectWriter writer)
        {
            Guard.NotNull(writer, nameof(writer));

            if (traces.Count > 0)
            {
                writer.WriteObject("profiler", p =>
                {
                    foreach (var kvp in traces)
                    {
                        p.WriteObject(kvp.Key, k => k
                                      .WriteProperty("elapsedMsTotal", kvp.Value.Total)
                                      .WriteProperty("elapsedMsAvg", kvp.Value.Total / kvp.Value.Count)
                                      .WriteProperty("count", kvp.Value.Count));
                    }
                });
            }
        }
Exemplo n.º 11
0
        public void Append(IObjectWriter writer, SemanticLogLevel logLevel)
        {
            var httpContext = httpContextAccessor.HttpContext;

            if (string.IsNullOrEmpty(httpContext?.Request?.Method))
            {
                return;
            }

            Guid requestId;

            if (httpContext.Items.TryGetValue(nameof(requestId), out var requestIdvalue) && requestIdvalue is Guid requestIdValue)
            {
                requestId = requestIdValue;
            }
            else
            {
                httpContext.Items[nameof(requestId)] = requestId = Guid.NewGuid();
            }

            var logContext = (requestId, context : httpContext, actionContextAccessor);

            writer.WriteObject("web", logContext, (ctx, w) =>
            {
                w.WriteProperty("requestId", ctx.requestId.ToString());
                w.WriteProperty("requestPath", ctx.context.Request.Path);
                w.WriteProperty("requestMethod", ctx.context.Request.Method);

                var actionContext = ctx.actionContextAccessor.ActionContext;

                if (actionContext != null)
                {
                    w.WriteObject("routeValues", actionContext.ActionDescriptor.RouteValues, (routeValues, r) =>
                    {
                        foreach (var kvp in routeValues)
                        {
                            r.WriteProperty(kvp.Key, kvp.Value);
                        }
                    });
                }
            });
        }
Exemplo n.º 12
0
    public static void DumpRequestContext(HttpContext httpContext, IDictionary <string, string?> routeValues, IObjectWriter logEntry)
    {
        if (!(httpContext.Items.TryGetValue(nameof(RequestContextAppender), out object?rawRequestId) && rawRequestId is Guid requestId))
        {
            httpContext.Items[nameof(RequestContextAppender)] = requestId = Guid.NewGuid();
        }

        logEntry.WriteObject("Request", x =>
        {
            x.WriteProperty("Id", requestId.ToString())
            .WriteProperty("Path", httpContext.Request.Path)
            .WriteProperty("Method", httpContext.Request.Method)
            .WriteObject("routeValues", y =>
            {
                foreach ((string key, string?value) in routeValues)
                {
                    y.WriteProperty(key, value);
                }
            });
        });
    }
Exemplo n.º 13
0
        public void Should_write_nested_object()
        {
            var result = sut.WriteObject("property", a => a.WriteProperty("nested", "my-string")).ToString();

            Assert.Equal(@"{""property"":{""nested"":""my-string""}}", result);
        }