GitHub.Observability.LoggingMiddleware
1.0.0
dotnet add package GitHub.Observability.LoggingMiddleware --version 1.0.0
NuGet\Install-Package GitHub.Observability.LoggingMiddleware -Version 1.0.0
<PackageReference Include="GitHub.Observability.LoggingMiddleware" Version="1.0.0" />
paket add GitHub.Observability.LoggingMiddleware --version 1.0.0
#r "nuget: GitHub.Observability.LoggingMiddleware, 1.0.0"
// Install GitHub.Observability.LoggingMiddleware as a Cake Addin
#addin nuget:?package=GitHub.Observability.LoggingMiddleware&version=1.0.0
// Install GitHub.Observability.LoggingMiddleware as a Cake Tool
#tool nuget:?package=GitHub.Observability.LoggingMiddleware&version=1.0.0
LoggingMiddleware
This package can be used add to a middleware to catch all the exceptions and to generate traces and metrics. It can also be used to emit traces/metrics by directly invoking TraceLogger
and MetricsLogger
.
Components
Please go through the sample project for better understanding of the usage.
LoggingMiddleware
This will capture RequestDelegate
and see if any exception is thrown. Based on the outcome a trace and a metric is emitted. This internally uses TraceLogger and MetricLogger.
Usage
Add this in Configure method of Startup.cs
app.UseMiddleware<LoggingMiddleware>(Constants.LogFormat.EqualSeparatedString, true);
- First parameter is of type enum LogFormat which can be accessed by importing Helpers class from LoggingMiddleware. Default value is set to
EqualSeparatedString
. More on this later. - Second parameter is of type bool which instructs the middleware whether to log metrics or not. This should only be set to true if all the necessary Env variables are set. Default value is set to
false
.
This step will enable logging of basic traces and metrics with predefined values.
TraceLogger
This class is used to log traces. We support logging of both Json type and equal separated strings. EG -
Json
{ExecutionTime: 121, StatusCode: 200, Status: 1}
This can be used with
Geneva
as json is one of the supported indexing mechanism.EqualSeparatedString
ExecutionTime=121 StatusCode=200 Status=1
This can be used to support indexing in
Splunk
.
TraceLogger is a static class based on Factory Pattern. In order to get correct instance use build method with appropriate parameters.
Build(Constants.LogFormat logFormat, ILogger logger)
This will return a TraceLogger object based on logFormat. Then use LogTrace to log.
LogTrace(int tracePoint, string trace, System.Diagnostics.TraceLevel level, string areaName, string layer, Dictionary<string, string> parameters = null);
Last parameters is optional which can be used to add more fields in logs other than few of which are already defined.
MetricLogger
This class is used to log metrics. This emits StatsD logs which can then be intergrated with Geneva. We only support Gauge
at the moment. For setting up this first we need to create Env variables.
- STATSD_PORT - This will be used to config StatsD publisher for this port. Default value for this is set to
8125
. - STATSD_HOST - This will be used to config the StatsD publisher. Default value is set to
geneva-services.geneva
. - MDM_NAMESPACE - This will be used to add namespace to all metrics generated. Default value for this based on app name (
AppDomain.CurrentDomain.FriendlyName
).
Gauge method accepts three parameters.
Gauge(string metricName, Dictionary<object, object> dimension, double value)
This generates a metric string and assign a value which can then be used to perform analytics on the data based on dimensions added.
Optional Configurations
The functionality loggingMiddleware can be imrpoved by adding LogParameters and MetricDimensions. Default logs for
Trace
By default we log
- TracePoint - This is set to "-1" for logs generated by LoggingMiddleware.
- Trace - This is a combination of error message and stace trace, in case of exceptions and left blank if request is successful.
- Level - This is set to Error in case of exception and Info otherwise.
- AreaName - Set to "Logging Middleware"
- Layer - Left blank
- StatusCode - Set to 500, in case of exception otherwise set to outcoming HttpStatusCode.
- ExecutionTime - A timer is started as soon as the request reaches middleware and stopped just before adding Trace. This time is logged here.
- Status - This is set to 0 if request is successful else -1
If we want to add more fields like Controller which was called or what action was taken, all this can be added in a dictionary which is passed using HttpContext.Items["LogParameters"]
.
Metrics
Similar to Trace, what dimensions to use for metrics can be configured through .HttpContext.Items["MetricDimensions"]
.
Please refer Filters in Sample Project. In case you want to use ActionFilter to pass the above values, do not forget to add this in ConfigureServices in startup.cs
services.AddControllers(options =>
{
options.Filters.Add(typeof(<FilterName>));
});
Product | Versions Compatible and additional computed target framework versions. |
---|---|
.NET | net5.0 is compatible. net5.0-windows was computed. net6.0 was computed. net6.0-android was computed. net6.0-ios was computed. net6.0-maccatalyst was computed. net6.0-macos was computed. net6.0-tvos was computed. net6.0-windows was computed. net7.0 was computed. net7.0-android was computed. net7.0-ios was computed. net7.0-maccatalyst was computed. net7.0-macos was computed. net7.0-tvos was computed. net7.0-windows was computed. net8.0 was computed. net8.0-android was computed. net8.0-browser was computed. net8.0-ios was computed. net8.0-maccatalyst was computed. net8.0-macos was computed. net8.0-tvos was computed. net8.0-windows was computed. |
-
net5.0
- JustEat.StatsD (>= 4.2.0)
- Microsoft.AspNetCore.Http (>= 2.2.2)
- Microsoft.Extensions.Logging (>= 5.0.0)
- Newtonsoft.Json (>= 12.0.3)
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.
Version | Downloads | Last updated |
---|---|---|
1.0.0 | 326 | 3/30/2021 |