🚀 Big News: Socket Acquires Coana to Bring Reachability Analysis to Every Appsec Team.Learn more
Socket
Book a DemoInstallSign in
Socket

elando.ELK.TraceLogging

Package Overview
Dependencies
Maintainers
1
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install
Package version was removed
This package version has been unpublished, mostly likely due to security reasons

elando.ELK.TraceLogging

Trace logging in to microservices with custom Http Header

6.0.0
unpublished
NuGet
Maintainers
1
Created
Source

Package for Request Tracing with Serilog

This package provides an easy configuration to trace HTTP requests using Serilog. Please follow the steps below to set it up.

Configuration Steps

1. Configure Serilog for Elasticsearch

In order to use this package, you need to configure Serilog to work with Elasticsearch. You can do this by calling AddElasticLogging() in SerilogConfigurationExtensions.cs. Additionally, make sure to provide the following configuration parameters:

  • elasticUri
  • indexPrefix
  • minLoggingLevel

OR

UseDirect Host Configuration about Serilog (with Elastic). You can do this by calling builder.Host.AddLogger() in HostExtension.cs. Additionally, make sure to provide the following configuration parameters:

  • builder.Configuration
  • LogEventLevel logLevel = LogEventLevel.Information

2. Add configuration property for header name into appsettings.json

{
  "TraceIdKey": "Your-Custom_Key-Name",
  "ElasticUri": "your.elastic.uri/",
  "LoggerPrefix": "Your-Custom_Prefix",
  "GlobalLogingFilter": "Your-Custom_Loging-Filter" // Optional
}

3. Add HttpContextAccessor via Dependency Injection

Each middleware in this package requires access to the HTTP context. To provide access, add the HttpContextAccessor to your DI container using:

builder.Services.AddHttpContextAccessor();

4. Configure HTTP Logging

Configure HTTP request/response logging using the following code:

builder.Services.AddHttpLogging(logging =>
{
    logging.RequestHeaders.Add(builder.Configuration.GetHeaderName());
});

5. Use Trace Identifier Middleware

Not needed for application inside the kube8. Add the TraceIdentifierMiddleware as the first middleware in your application to include a new trace header with a unique GUID:

app.UseMiddleware<HttpTraceMiddleware>();

6. Use HTTP Logging Middleware

Use UseHttpLogging() as the second middleware in your application to log the HTTP requests and responses:

app.UseHttpLogging();

7. Additional Configuration

Not needed for application inside the kube8. If you need to trace:

7.1. gRPC Calls

Add the TraceIdentifierInterceptor to your services:

services.AddSingleton<HttpTraceInterceptor>();

Configure each gRPC client with the TraceIdentifierInterceptor:

services.AddGrpcClient<gRPCClient>(options => ...)
        .AddInterceptor<HttpTraceInterceptor>();

7.2. HTTP Requests

Note that HTTP requests are not implemented yet and require further development.

8. Logging Extensions

Use LoggerTraceExtensions based on 'Microsoft.Extensions.Logging.ILogger'.

8.1 Example: How to use LoggerTraceExtensions.

var traceId = this.HttpContext.GetTraceId();
_logger.LogModelWithDepthOne(request, traceId);

The traceId argument enables tracing of the log in an application & across microservice architecture.

8.2 Example: Redact sensitive data - object

var traceId = this.HttpContext.GetTraceId();
_logger.LogModelWithDepthOne(model, traceId, nameof(model.Phone));

8.3 Example: Redact sensitive data - collection

var traceId = this.HttpContext.GetTraceId();

// New code
IEnumerable<TypeModel> copy = model.DeepCopy();
copy.RedactSensitiveData(nameof(TypeModel.Address), nameof(TypeModel.Phone));

_logger.LogModelWithDepthOne(copy, traceId);

Keywords

Logging

FAQs

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts