NDB.Logging.Api
parent
b2a0d582a4
commit
40f104e11e
|
@ -4,5 +4,6 @@
|
|||
<Authors>Tudor Stanciu</Authors>
|
||||
<Company>STA</Company>
|
||||
<PackageTags>NDB</PackageTags>
|
||||
<Copyright>STA NDB</Copyright>
|
||||
</PropertyGroup>
|
||||
</Project>
|
|
@ -0,0 +1,16 @@
|
|||
<Project Sdk="Microsoft.NET.Sdk">
|
||||
|
||||
<PropertyGroup>
|
||||
<TargetFramework>netstandard2.0</TargetFramework>
|
||||
<PackageProjectUrl>https://dev.azure.com/tstanciu94/NDB</PackageProjectUrl>
|
||||
<Description>.NET standard library for logging stuff in an API.</Description>
|
||||
<RepositoryUrl>https://dev.azure.com/tstanciu94/_git/NDB</RepositoryUrl>
|
||||
<PackageReleaseNotes>.NET standard library for logging stuff in an API.</PackageReleaseNotes>
|
||||
<PackageTags>NDB NDB.Logging</PackageTags>
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<PackageReference Include="Microsoft.AspNetCore.Mvc.Core" Version="2.2.5" />
|
||||
</ItemGroup>
|
||||
|
||||
</Project>
|
|
@ -0,0 +1,66 @@
|
|||
using Microsoft.AspNetCore.Http.Internal;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
using Microsoft.AspNetCore.Mvc.Filters;
|
||||
using Microsoft.Extensions.Logging;
|
||||
using System;
|
||||
using System.Diagnostics;
|
||||
using System.IO;
|
||||
using System.Net;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace NDB.Logging.Api
|
||||
{
|
||||
public class RequestLoggingAttribute : ActionFilterAttribute
|
||||
{
|
||||
private readonly ILogger<RequestLoggingAttribute> _logger;
|
||||
|
||||
public RequestLoggingAttribute(ILogger<RequestLoggingAttribute> logger)
|
||||
{
|
||||
_logger = logger;
|
||||
}
|
||||
|
||||
public override async Task OnActionExecutionAsync(ActionExecutingContext context, ActionExecutionDelegate next)
|
||||
{
|
||||
var stopWatch = new Stopwatch();
|
||||
stopWatch.Start();
|
||||
|
||||
var uid = Guid.NewGuid();
|
||||
var request = ((DefaultHttpRequest)((ControllerBase)context.Controller).Request);
|
||||
var requestUrl = request.Path;
|
||||
|
||||
if (request.QueryString != null && request.QueryString.HasValue)
|
||||
requestUrl += request.QueryString.ToString();
|
||||
|
||||
var logContent = $"Start request {uid} for method {requestUrl}";
|
||||
|
||||
if (request.Method == WebRequestMethods.Http.Post)
|
||||
{
|
||||
var requestBody = await GetRequestBody(request);
|
||||
logContent += $" with body {requestBody}";
|
||||
}
|
||||
|
||||
_logger.LogInformation(logContent);
|
||||
|
||||
var resultContext = await next();
|
||||
stopWatch.Stop();
|
||||
|
||||
if (resultContext.Exception != null)
|
||||
_logger.LogError(resultContext.Exception, $"Request {uid} for method {requestUrl} failed - {resultContext.Exception.Message}");
|
||||
|
||||
_logger.LogInformation($"End request {uid} for method {requestUrl}");
|
||||
_logger.LogDebug($"Request {uid} duration: {stopWatch.ElapsedMilliseconds:N0} ms");
|
||||
}
|
||||
|
||||
private async Task<string> GetRequestBody(DefaultHttpRequest request)
|
||||
{
|
||||
if (request.ContentLength == 0)
|
||||
return "-";
|
||||
|
||||
var body = new StreamReader(request.Body);
|
||||
body.BaseStream.Seek(0, SeekOrigin.Begin);
|
||||
var bodyStr = await body.ReadToEndAsync();
|
||||
|
||||
return bodyStr;
|
||||
}
|
||||
}
|
||||
}
|
8
NDB.sln
8
NDB.sln
|
@ -15,7 +15,9 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Solution Items", "Solution
|
|||
EndProject
|
||||
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "NDB.Infrastructure.PublicIP", "NDB.Infrastructure.PublicIP\NDB.Infrastructure.PublicIP.csproj", "{FCB29735-E36A-442A-9307-F124782B8BB8}"
|
||||
EndProject
|
||||
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "NDB.Testing.App", "NDB.Testing.App\NDB.Testing.App.csproj", "{656CCE38-90C3-4D19-9D07-EF1F0410CFDC}"
|
||||
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "NDB.Testing.App", "NDB.Testing.App\NDB.Testing.App.csproj", "{656CCE38-90C3-4D19-9D07-EF1F0410CFDC}"
|
||||
EndProject
|
||||
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "NDB.Logging.Api", "NDB.Logging.Api\NDB.Logging.Api.csproj", "{74E221BE-C097-468B-93E4-AAE1B2173C49}"
|
||||
EndProject
|
||||
Global
|
||||
GlobalSection(SolutionConfigurationPlatforms) = preSolution
|
||||
|
@ -35,6 +37,10 @@ Global
|
|||
{656CCE38-90C3-4D19-9D07-EF1F0410CFDC}.Debug|Any CPU.Build.0 = Debug|Any CPU
|
||||
{656CCE38-90C3-4D19-9D07-EF1F0410CFDC}.Release|Any CPU.ActiveCfg = Release|Any CPU
|
||||
{656CCE38-90C3-4D19-9D07-EF1F0410CFDC}.Release|Any CPU.Build.0 = Release|Any CPU
|
||||
{74E221BE-C097-468B-93E4-AAE1B2173C49}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
|
||||
{74E221BE-C097-468B-93E4-AAE1B2173C49}.Debug|Any CPU.Build.0 = Debug|Any CPU
|
||||
{74E221BE-C097-468B-93E4-AAE1B2173C49}.Release|Any CPU.ActiveCfg = Release|Any CPU
|
||||
{74E221BE-C097-468B-93E4-AAE1B2173C49}.Release|Any CPU.Build.0 = Release|Any CPU
|
||||
EndGlobalSection
|
||||
GlobalSection(SolutionProperties) = preSolution
|
||||
HideSolutionNode = FALSE
|
||||
|
|
Loading…
Reference in New Issue