public HomeController(
     ILogger <HomeController> logger,
     IDiagnosticContext diagnosticContext)
 {
     Logger            = logger;
     DiagnosticContext = diagnosticContext;
 }
        public static void EnrichFromRequest(IDiagnosticContext diagnosticContext, HttpContext httpContext)
        {
            var request = httpContext.Request;

            // Set all the common properties available for every request
            diagnosticContext.Set(Microsoft.Net.Http.Headers.HeaderNames.Host, request.Host);
            diagnosticContext.Set(Invariants.AspNet.Request.Protocol, request.Protocol);
            diagnosticContext.Set(Invariants.AspNet.Request.Scheme, request.Scheme);

            // Only set it if available. You're not sending sensitive data in a querystring right?!
            if (request.QueryString.HasValue)
            {
                diagnosticContext.Set("QueryString", request.QueryString.Value);
            }

            // Set the content-type of the Response at this point
            diagnosticContext.Set(Microsoft.Net.Http.Headers.HeaderNames.ContentType, httpContext.Response.ContentType);

            // Retrieve the IEndpointFeature selected for the request
            var endpoint = httpContext.GetEndpoint();

            if (!ReferenceEquals(endpoint, null))
            {
                diagnosticContext.Set(Invariants.AspNet.Request.EndpointName, endpoint.DisplayName);
            }
        }
Ejemplo n.º 3
0
        public static void EnrichFromRequest(IDiagnosticContext diagnosticContext, HttpContext httpContext)
        {
            var request = httpContext.Request;

            // Set all the common properties available for every request
            diagnosticContext.Set("RemoteIpAddress", httpContext.Connection.RemoteIpAddress.ToString());
            diagnosticContext.Set("Host", request.Host);
            diagnosticContext.Set("Protocol", request.Protocol);
            diagnosticContext.Set("Scheme", request.Scheme);

            // Only set it if available. You're not sending sensitive data in a querystring right?!
            if (request.QueryString.HasValue)
            {
                diagnosticContext.Set("QueryString", request.QueryString.Value);
            }

            //diagnosticContext.Set("Query", httpContext.Request.Query.ToDictionary(x => x.Key, y => y.Value.ToString()), true);
            //diagnosticContext.Set("Headers", httpContext.Request.Headers.ToDictionary(x => x.Key, y => y.Value.ToString()), true);
            //diagnosticContext.Set("Cookies", httpContext.Request.Cookies.ToDictionary(x => x.Key, y => y.Value.ToString()), true);

            // Set the content-type of the Response at this point
            diagnosticContext.Set("ContentType", httpContext.Response.ContentType);

            diagnosticContext.Set("UserInfo", GetUserInfo(httpContext.User), true);


            // Retrieve the IEndpointFeature selected for the request
            var endpoint = httpContext.GetEndpoint();

            if (endpoint is object) // endpoint != null
            {
                diagnosticContext.Set("EndpointName", endpoint.DisplayName);
            }
        }
Ejemplo n.º 4
0
 public GuiaController(ILogger <GuiaController> logger, IDiagnosticContext diagnosticContext,
                       IGuiaRepository GuiaRepository,
                       IGuiaNumeroRepository GuiaNumeroRepository,
                       IGuiaService GuiaService,
                       IPrestadorService PrestadorService,
                       IBeneficiarioService AssociadoService,
                       IPushService PushService,
                       ITokenService TokenService,
                       IUnitOfWork uow,
                       IPushRequest pushRequest,
                       IMapper mapper)
 {
     _logger               = logger ?? throw new ArgumentNullException(nameof(logger));
     _diagnosticContext    = diagnosticContext ?? throw new ArgumentNullException(nameof(diagnosticContext));
     _GuiaRepository       = GuiaRepository;
     _GuiaNumeroRepository = GuiaNumeroRepository;
     _GuiaService          = GuiaService;
     _PrestadorService     = PrestadorService;
     _BeneficiarioService  = AssociadoService;
     _PushService          = PushService;
     _TokenService         = TokenService;
     _uow         = uow;
     _mapper      = mapper;
     _PushRequest = pushRequest;
 }
Ejemplo n.º 5
0
        public static async void EnrichFromRequest(IDiagnosticContext diagnosticContext, HttpContext httpContext)
        {
            var request = httpContext.Request;

            diagnosticContext.Set("RequestBody", RequestPayload);

            string responseBodyPayload = await ReadResponseBody(httpContext.Response);

            diagnosticContext.Set("ResponseBody", responseBodyPayload);

            diagnosticContext.Set("Host", request.Host);
            diagnosticContext.Set("Protocol", request.Protocol);
            diagnosticContext.Set("Scheme", request.Scheme);

            if (request.QueryString.HasValue)
            {
                diagnosticContext.Set("QueryString", request.QueryString.Value);
            }

            diagnosticContext.Set("ContentType", httpContext.Response.ContentType);

            var endpoint = httpContext.GetEndpoint();

            if (endpoint is object) // endpoint != null
            {
                diagnosticContext.Set("EndpointName", endpoint.DisplayName);
            }
        }
Ejemplo n.º 6
0
        public CreateCart(ILogger <CreateCart> logger, IDiagnosticContext diagnosticsContext, IStringLocalizer <Program> localizer, IClock clock, CartContext context)
        {
            if (logger is null)
            {
                throw new ArgumentNullException(nameof(logger));
            }

            if (diagnosticsContext is null)
            {
                throw new ArgumentNullException(nameof(diagnosticsContext));
            }

            if (localizer is null)
            {
                throw new ArgumentNullException(nameof(localizer));
            }

            if (clock is null)
            {
                throw new ArgumentNullException(nameof(clock));
            }

            if (context is null)
            {
                throw new ArgumentNullException(nameof(context));
            }

            Logger             = logger;
            DiagnosticsContext = diagnosticsContext;
            Localizer          = localizer;
            Clock   = clock;
            Context = context;
        }
        public DiagnosticMenuTag(IDiagnosticContext context, ICurrentHttpRequest currentHttpRequest, IUrlRegistry urls) : base("ul")
        {
            AddClass("nav");

            var group = context.CurrentGroup();

            var index = group.Index();
            if (index != null)
            {
                addLink(index, context, currentHttpRequest);
            }
            else
            {
                var url = urls.UrlFor(new GroupRequest {Name = group.Name});
                var li = Add("li");
                li.Add("a").Attr("href", url).Text(group.Name).Attr("title", group.Description);

                if (context.CurrentChain() == null)
                {
                    li.AddClass("active");
                }
            }

            group.Links().Each(x => addLink(x, context, currentHttpRequest));
        }
Ejemplo n.º 8
0
        public override void OnActionExecuting(ActionExecutingContext context)
        {
            IDiagnosticContext diagnosticContext = context.HttpContext.RequestServices.GetService <IDiagnosticContext>();

            diagnosticContext.Set("ActionName", context.ActionDescriptor.DisplayName);
            diagnosticContext.Set("ActionId", context.ActionDescriptor.Id);
        }
Ejemplo n.º 9
0
 public static void EnrichFromRequest(IDiagnosticContext diagnosticContext, HttpContext httpContext)
 {
     diagnosticContext.Set("UserName", httpContext?.User?.Identity?.Name);
     diagnosticContext.Set("ClientIP", httpContext.Connection.RemoteIpAddress.ToString());
     diagnosticContext.Set("UserAgent", httpContext.Request.Headers["User-Agent"].FirstOrDefault());
     diagnosticContext.Set("Resource", httpContext.GetMetricsCurrentResourceName());
 }
Ejemplo n.º 10
0
        public static void EnrichFromRequest(IDiagnosticContext diagnosticContext, HttpContext httpContext)
        {
            var request = httpContext.Request;

            // Set all the common properties available for every request
            diagnosticContext.Set("Host", request.Host);
            diagnosticContext.Set("Protocol", request.Protocol);
            diagnosticContext.Set("Scheme", request.Scheme);

            // Only set it if available. You're not sending sensitive data in a querystring right?!
            if (request.QueryString.HasValue)
            {
                diagnosticContext.Set("QueryString", request.QueryString.Value);
            }

            // Set the content-type of the Response at this point
            diagnosticContext.Set("ContentType", httpContext.Response.ContentType);

            // Retrieve the IEndpointFeature selected for the request
            var endpoint = httpContext.GetEndpoint();

            if (endpoint is object) // endpoint != null
            {
                diagnosticContext.Set("EndpointName", endpoint.DisplayName);
            }
        }
Ejemplo n.º 11
0
        public static void EnrichFromRequest(
            IDiagnosticContext diagnosticContext, HttpContext httpContext)
        {
            var request = httpContext.Request;

            diagnosticContext.Set("Host", request.Host);
            diagnosticContext.Set("Protocol", request.Protocol);
            diagnosticContext.Set("Scheme", request.Scheme);
            if (request.QueryString.HasValue)
            {
                diagnosticContext.Set("QueryString", request.QueryString.Value);
            }

            diagnosticContext.Set("ContentType", httpContext.Response.ContentType);
            diagnosticContext.Set("UserAgent", httpContext.Request.Headers["User-Agent"].ToString());
            diagnosticContext.Set("ClientIP", httpContext.Connection.RemoteIpAddress);
            diagnosticContext.Set("UserName", httpContext.User.Identity.Name == null ? "(anonymous)" : httpContext.User.Identity.Name);

            var clientIdClaim = httpContext.User.FindFirst("client_id");

            if (clientIdClaim != null)
            {
                diagnosticContext.Set("OAuthClientId", clientIdClaim.Value);
            }
        }
 public HomeController(
     ILogger <HomeController> logger,
     IDiagnosticContext diagnosticContext)
 {
     Logger            = logger ?? throw new ArgumentNullException(nameof(logger));
     DiagnosticContext = diagnosticContext ?? throw new ArgumentNullException(nameof(diagnosticContext));
 }
Ejemplo n.º 13
0
        public static void EnrichFromRequest(IDiagnosticContext diagnosticContext, HttpContext httpContext)
        {
            var request = httpContext.Request;

            diagnosticContext.Set("Host", request.Host);
            diagnosticContext.Set("Protocol", request.Protocol);
            diagnosticContext.Set("Scheme", request.Scheme);

            if (request.QueryString.HasValue)
            {
                diagnosticContext.Set("QueryString", request.QueryString.Value);
            }

            diagnosticContext.Set("ContentType", httpContext.Response.ContentType);

            var endpoint = httpContext.GetEndpoint();

            if (endpoint is not null) // endpoint != null
            {
                diagnosticContext.Set("EndpointName", endpoint.DisplayName);
            }

            var headers = request.Headers;

            if (headers.Any())
            {
                diagnosticContext.Set("Headers", headers);
            }
        }
Ejemplo n.º 14
0
 public TranslationTransformer(IServiceScopeFactory serviceScopeFactory, ILogger <TranslationTransformer> logger, IDiagnosticContext diagnosticContext)
 {
     _scopefactory = serviceScopeFactory;
     _logger       = logger;
     //  _dbContext = new ApplicationDbContext(dbContext);
     _diagnosticContext = diagnosticContext ?? throw new ArgumentNullException(nameof(diagnosticContext));
 }
Ejemplo n.º 15
0
 /// <summary>
 /// FilterContext log enrichments used on action filters etc.
 /// </summary>
 /// <param name="diagnosticContext"></param>
 /// <param name="context"></param>
 public static void EnrichFromFilterContext(IDiagnosticContext diagnosticContext, FilterContext context)
 {
     diagnosticContext.Set("RouteData", context?.ActionDescriptor?.RouteValues);
     diagnosticContext.Set("ActionName", context?.ActionDescriptor?.DisplayName);
     diagnosticContext.Set("ActionId", context?.ActionDescriptor?.Id);
     diagnosticContext.Set("ValidationState", context?.ModelState?.IsValid);
 }
Ejemplo n.º 16
0
        public static void EnrichFromRequest(IDiagnosticContext diagnosticContext, HttpContext httpContext)
        {
            var request = httpContext.Request;

            // 为每个请求都设置通用的属性
            diagnosticContext.Set("Host", request.Host);
            diagnosticContext.Set("Protocol", request.Protocol);
            diagnosticContext.Set("Scheme", request.Scheme);
            diagnosticContext.Set("RemoteIpAddress", httpContext.Connection.RemoteIpAddress);
            // 如果要记录 Request Body 或 Response Body
            // 参考 https://stackoverflow.com/questions/60076922/serilog-logging-web-api-methods-adding-context-properties-inside-middleware
            string requestBody = ReadRequestBody(httpContext.Request).Result;

            if (!string.IsNullOrEmpty(requestBody))
            {
                diagnosticContext.Set("RequestBody", requestBody);
            }

            // string responseBody = ReadResponseBody(httpContext.Response).Result;
            // if (!string.IsNullOrEmpty(responseBody))
            // {
            //     diagnosticContext.Set("ResponseBody", requestBody);
            // }

            if (request.QueryString.HasValue)
            {
                diagnosticContext.Set("QueryString", request.QueryString.Value);
            }
        }
Ejemplo n.º 17
0
        public GetCartById(ILogger <GetCartById> logger, IDiagnosticContext diagnosticsContext, IStringLocalizer <Program> localizer, IMapper mapper, CartContext context)
        {
            if (logger is null)
            {
                throw new ArgumentNullException(nameof(logger));
            }

            if (diagnosticsContext is null)
            {
                throw new ArgumentNullException(nameof(diagnosticsContext));
            }

            if (localizer is null)
            {
                throw new ArgumentNullException(nameof(localizer));
            }

            if (mapper is null)
            {
                throw new ArgumentNullException(nameof(mapper));
            }

            if (context is null)
            {
                throw new ArgumentNullException(nameof(context));
            }

            Logger             = logger;
            DiagnosticsContext = diagnosticsContext;
            Localizer          = localizer;
            Mapper             = mapper;
            Context            = context;
        }
Ejemplo n.º 18
0
        public AdminController(LoggingLevelSwitch loggingLevelSwitch, IDiagnosticContext diagnosticContext)
        {
            _loggingLevelSwitch = loggingLevelSwitch;
            _diagnosticContext  = diagnosticContext;

            // {SourceContext} property in the outputTemplate, can be populated with the class name.
            //Log.ForContext<AdminController>().Debug("Constructor: AdminController");
        }
Ejemplo n.º 19
0
        /// <summary>
        /// Enriches the HTTP request log with additional data via the Diagnostic Context
        /// </summary>
        /// <param name="diagnosticContext">The Serilog diagnostic context</param>
        /// <param name="httpContext">The current HTTP Context</param>
        /// NOTE(collin): https://benfoster.io/blog/serilog-best-practices/#request-log-enricher
        public static void EnrichFromRequest(IDiagnosticContext diagnosticContext, HttpContext httpContext)
        {
            diagnosticContext.Set("ClientIp", httpContext.Connection.RemoteIpAddress.ToString());
            diagnosticContext.Set("UserAgent", httpContext.Request.Headers["User-Agent"].FirstOrDefault());

            // NOTE(collin): enable if you want to specify an alternate name in the [Http*] attributes for endpoints
            //diagnosticContext.Set("Resource", httpContext.GetMetricsCurrentResourceName());
        }
Ejemplo n.º 20
0
 public AccountController(UserManager <InfoUserModel> userManager, SignInManager <InfoUserModel> signInManager, IHostingEnvironment hostingEnvironment, IDiagnosticContext diagnosticContext, ILogger <AccountController> logger)
 {
     this.userManager         = userManager;
     this.signInManager       = signInManager;
     this._hostingEnvironment = hostingEnvironment;
     this._diagnosticContext  = diagnosticContext;
     this._logger             = logger;
 }
Ejemplo n.º 21
0
 /// <summary>
 /// Controller constructor
 /// </summary>
 /// <param name="db">IDImagerDB</param>
 /// <param name="serviceSettings">ServiceSettings</param>
 /// <param name="logger">Logger</param>
 /// <param name="diagnosticContext">Logger diagnostic context</param>
 public MediaController(IDImagerDB db, ServiceSettings serviceSettings,
                        ILogger <MediaController> logger, IDiagnosticContext diagnosticContext)
 {
     this.db = db ?? throw new ArgumentNullException(nameof(logger));
     this.serviceSettings   = serviceSettings ?? throw new ArgumentNullException(nameof(serviceSettings));
     this.logger            = logger ?? throw new ArgumentNullException(nameof(logger));
     this.diagnosticContext = diagnosticContext ?? throw new ArgumentNullException(nameof(diagnosticContext));
 }
Ejemplo n.º 22
0
 public WebhookController(IOptions <ChangelogConfig> cfg, IDiagnosticContext context,
                          ChangelogService changelogService, ILogger <WebhookController> log)
 {
     _cfg              = cfg;
     _context          = context;
     _changelogService = changelogService;
     _log              = log;
 }
Ejemplo n.º 23
0
 public ActionTrackFilterAttribute(
     IDiagnosticContext diagnosticContext,
     IFreeSqlRepository <ActionTrackLog> actionTrackLogRepository,
     IHttpContextAccessor httpContextAccessor)
 {
     _diagnosticContext        = diagnosticContext;
     _actionTrackLogRepository = actionTrackLogRepository;
     _httpContextAccessor      = httpContextAccessor;
 }
 public HomeController(
     ILogger <HomeController> logger,
     IDiagnosticContext diagnosticContext,
     LoggerProviderCollection loggerProviderCollection)
 {
     Logger                   = logger ?? throw new ArgumentNullException(nameof(logger));
     DiagnosticContext        = diagnosticContext ?? throw new ArgumentNullException(nameof(diagnosticContext));
     LoggerProviderCollection = loggerProviderCollection ?? throw new ArgumentNullException(nameof(loggerProviderCollection));
 }
Ejemplo n.º 25
0
 public HomeController(
     ILogger <HomeController> logger,
     IDiagnosticContext diagnosticContext,
     IShoppingCartService shoppingCartService)
 {
     Logger              = logger ?? throw new ArgumentNullException(nameof(logger));
     DiagnosticContext   = diagnosticContext ?? throw new ArgumentNullException(nameof(diagnosticContext));
     ShoppingCartService = shoppingCartService ?? throw new ArgumentNullException(nameof(shoppingCartService));
 }
Ejemplo n.º 26
0
 public HttpLoggingMiddleware(
     ILogger <HttpLoggingMiddleware> logger,
     RequestDelegate next,
     IDiagnosticContext diagnosticContext)
 {
     this.logger            = logger;
     this.next              = next;
     this.diagnosticContext = diagnosticContext;
 }
Ejemplo n.º 27
0
        public static void EnrichFromRequest(IDiagnosticContext diagnosticContext, HttpContext httpContext)
        {
            diagnosticContext.Set("UserName", httpContext.User.Identity.Name);

            // Retrieve the IEndpointFeature selected for the request
            var endpoint = httpContext.GetEndpoint();

            diagnosticContext.Set("EndpointName", endpoint?.DisplayName);
        }
Ejemplo n.º 28
0
        public static void EnrichFromRequest(IDiagnosticContext diagnosticContext, HttpContext httpContext)
        {
            // Set the version attributes
            var             assembly             = Assembly.GetExecutingAssembly();
            string?         informationalVersion = assembly.GetCustomAttribute <AssemblyInformationalVersionAttribute>()?.InformationalVersion;
            var             assemblyVersion      = assembly.GetName().Version?.ToString();
            FileVersionInfo fileVersionInfo      = FileVersionInfo.GetVersionInfo(assembly.Location);

            diagnosticContext.Set("InformationalVersion", informationalVersion);
            diagnosticContext.Set("AssemblyVersion", assemblyVersion);
            diagnosticContext.Set("AssemblyFileVersion", fileVersionInfo.FileVersion);

            HttpRequest request = httpContext.Request;

            // Set all the common properties available for every request
            diagnosticContext.Set("Host", request.Host);
            diagnosticContext.Set("Protocol", request.Protocol);
            diagnosticContext.Set("Scheme", request.Scheme);
            diagnosticContext.Set("HttpMode", "INBOUND");

            // Log Request Headers
            Dictionary <string, StringValues> requestHeaders = request.Headers.ToDictionary(
                i => i.Key,
                i => i.Value);

            // remove sensitive Authorization header because it contains the JWT token with personal data
            requestHeaders.Remove("Authorization");
            diagnosticContext.Set("RequestHeaders", requestHeaders, true);

            // Only set it if available. You're not sending sensitive data in a querystring right?!
            if (request.QueryString.HasValue)
            {
                diagnosticContext.Set("QueryString", request.QueryString.Value);
            }

            // Set the content-type of the Response at this point
            diagnosticContext.Set("ContentType", httpContext.Response.ContentType);

            // Retrieve the IEndpointFeature selected for the request
            Endpoint endpoint = httpContext.GetEndpoint();

            if (endpoint != null)
            {
                diagnosticContext.Set("EndpointName", endpoint.DisplayName);
            }

            HttpResponse response = httpContext.Response;

            // Set all the common properties available for every response

            // Log Response Headers
            Dictionary <string, StringValues> responseHeaders = request.Headers.ToDictionary(
                i => i.Key,
                i => i.Value);

            diagnosticContext.Set("ResponseHeaders", responseHeaders, true);
        }
Ejemplo n.º 29
0
        public override void OnActionExecuting(ActionExecutingContext context)
        {
            // Serilog does not include these by default but they are useful to add https://nblumhardt.com/2019/10/serilog-mvc-logging/
            //
            IDiagnosticContext diagnosticContext = context.HttpContext.RequestServices.GetService <IDiagnosticContext>();

            diagnosticContext.Set("ActionName", context.ActionDescriptor.DisplayName);
            diagnosticContext.Set("ActionId", context.ActionDescriptor.Id);
        }
Ejemplo n.º 30
0
    /// <summary>
    /// HttpContext log enrichments used in most cases
    /// </summary>
    /// <param name="diagnosticContext"></param>
    /// <param name="httpContext"></param>
    public static void EnrichFromContext(IDiagnosticContext diagnosticContext, HttpContext httpContext)
    {
        diagnosticContext.Set("IdentityName", httpContext?.User?.Identity?.Name);
        diagnosticContext.Set("RemoteIp", httpContext?.Connection?.RemoteIpAddress);

        if (httpContext?.Request?.Headers != null && httpContext.Request.Headers.ContainsKey(HeaderNames.UserAgent))
        {
            diagnosticContext.Set("Agent", httpContext.Request.Headers[HeaderNames.UserAgent]);
        }
    }
 public WeatherForecastController(
     ILogger <WeatherForecastController> logger,
     IDiagnosticContext diagnosticContext,
     DataContext context
     )
 {
     _logger            = logger ?? throw new ArgumentNullException(nameof(logger));
     _diagnosticContext = diagnosticContext ?? throw new ArgumentNullException(nameof(diagnosticContext));
     _context           = context;
 }
        private void addLink(DiagnosticChain diagnosticChain, IDiagnosticContext context, ICurrentHttpRequest currentHttpRequest)
        {
            var url = currentHttpRequest.ToFullUrl(diagnosticChain.GetRoutePattern());
            var li = Add("li");
            li.Add("a").Attr("href", url).Text(diagnosticChain.Title).Attr("title", diagnosticChain.Description);

            if (context.CurrentChain() == diagnosticChain)
            {
                li.AddClass("active");
            }
        }