コード例 #1
0
ファイル: LokiAttribute.cs プロジェクト: LokiLokus/LokiLogger
 public void Init(object instance, MethodBase method, object[] args)
 {
     _stopwatch  = new Stopwatch();
     _methodName = method.Name;
     _className  = method.DeclaringType.FullName;
     Loki.Write(Loki.INVOKE_TYP, LogLevel.SystemGenerated, null, _methodName, _className, -1, args);
 }
コード例 #2
0
        public async Task Invoke(HttpContext context)
        {
            if (!LokiObjectAdapter.LokiConfig.UseLokiMiddleware || LokiObjectAdapter.LokiConfig.IgnoreRoutes.Any(x => context.Request.Path.ToString().Contains(x)))
            {
                await _next(context);
            }
            else
            {
                WebRestLog log = new WebRestLog()
                {
                    TraceId = context.TraceIdentifier
                };
                try
                {
                    if (!LokiObjectAdapter.LokiConfig.NoRequestRoutes.Any(x => context.Request.Path.ToString().Contains(x)))
                    {
                        log = await LogRequest(context.Request, log);
                    }
                }
                catch (Exception e)
                {
                    Loki.ExceptionWarning("Error in Loki Middleware logging Request", e);
                }

                var originalBodyStream = context.Response.Body;

                //Create a new memory stream...
                using (var responseBody = new MemoryStream())
                {
                    context.Response.Body = responseBody;
                    try
                    {
                        await _next(context);

                        if (context.Items.ContainsKey("Exception"))
                        {
                            Exception ex = (Exception)context.Items["Exception"];
                            log.Exception = ex.Message + "\n" + ex.StackTrace + "\n" + ex.Source;
                        }
                    }
                    catch (Exception e)
                    {
                        log.Exception = e.Message + "\n" + e.StackTrace + "\n" + e.Source;
                        await LogResponse(context.Response, log);

                        Loki.Write(Loki.REST_TYP, LogLevel.Error, "", "Invoke", "LokiWebExtension.Middleware.LokiMiddleware", 48, log);
                        throw;
                    }

                    try
                    {
                        if (!LokiObjectAdapter.LokiConfig.NoResponseRoutes.Any(x => context.Request.Path.ToString().Contains(x)))
                        {
                            await LogResponse(context.Response, log);
                        }
                    }
                    catch (Exception e)
                    {
                        Loki.ExceptionWarning("Error in Loki Middleware logging Response", e);
                    }
                    if (originalBodyStream.CanWrite && responseBody.CanRead)
                    {
                        await responseBody.CopyToAsync(originalBodyStream);
                    }
                }


                Loki.Write(Loki.REST_TYP, LogLevel.SystemGenerated, "", "Invoke", "LokiWebExtension.Middleware.LokiMiddleware", 55, log);
            }
        }
コード例 #3
0
ファイル: LokiAttribute.cs プロジェクト: LokiLokus/LokiLogger
 public void OnException(Exception exception)
 {
     _stopwatch.Stop();
     Loki.Write(Loki.EXCEPTION_TYP, LogLevel.SystemGenerated, null, _methodName, _className, -1, exception);
 }
コード例 #4
0
ファイル: LokiAttribute.cs プロジェクト: LokiLokus/LokiLogger
 public void OnExit()
 {
     _stopwatch.Stop();
     Loki.Write(Loki.RETURN_TYP, LogLevel.SystemGenerated, null, _methodName, _className, -1, _stopwatch.ElapsedTicks);
 }