mirror of https://github.com/abpframework/abp.git
8 changed files with 98 additions and 124 deletions
@ -0,0 +1,86 @@ |
|||
using System; |
|||
using System.Threading.Tasks; |
|||
using Microsoft.AspNetCore.Http; |
|||
using Microsoft.Extensions.DependencyInjection; |
|||
using Microsoft.Extensions.Logging; |
|||
using Microsoft.Extensions.Logging.Abstractions; |
|||
using Volo.Abp.Auditing; |
|||
using Volo.Abp.DependencyInjection; |
|||
|
|||
namespace Volo.Abp.AspNetCore.Auditing |
|||
{ |
|||
public class AspNetCoreAuditLogContributor : AuditLogContributor, ITransientDependency |
|||
{ |
|||
public ILogger<AspNetCoreAuditLogContributor> Logger { get; set; } |
|||
|
|||
public AspNetCoreAuditLogContributor() |
|||
{ |
|||
Logger = NullLogger<AspNetCoreAuditLogContributor>.Instance; |
|||
} |
|||
|
|||
public override Task ContributeAsync(AuditLogContributionContext context) |
|||
{ |
|||
var httpContext = context.ServiceProvider.GetRequiredService<IHttpContextAccessor>().HttpContext; |
|||
if (httpContext == null) |
|||
{ |
|||
return Task.CompletedTask; |
|||
} |
|||
|
|||
if (context.AuditInfo.HttpMethod == null) |
|||
{ |
|||
context.AuditInfo.HttpMethod = httpContext.Request.Method; |
|||
} |
|||
|
|||
if (context.AuditInfo.Url == null) |
|||
{ |
|||
context.AuditInfo.Url = BuildUrl(httpContext); |
|||
} |
|||
|
|||
if (context.AuditInfo.ClientIpAddress == null) |
|||
{ |
|||
context.AuditInfo.ClientIpAddress = GetClientIpAddress(httpContext); |
|||
} |
|||
|
|||
if (context.AuditInfo.BrowserInfo == null) |
|||
{ |
|||
context.AuditInfo.BrowserInfo = GetBrowserInfo(httpContext); |
|||
} |
|||
|
|||
//TODO: context.AuditInfo.ClientName
|
|||
|
|||
return Task.CompletedTask; |
|||
} |
|||
|
|||
protected virtual string GetBrowserInfo(HttpContext httpContext) |
|||
{ |
|||
return httpContext?.Request?.Headers?["User-Agent"]; |
|||
} |
|||
|
|||
protected virtual string GetClientIpAddress(HttpContext httpContext) |
|||
{ |
|||
try |
|||
{ |
|||
return httpContext?.Connection?.RemoteIpAddress?.ToString(); |
|||
} |
|||
catch (Exception ex) |
|||
{ |
|||
Logger.LogException(ex, LogLevel.Warning); |
|||
return null; |
|||
} |
|||
} |
|||
|
|||
protected virtual string BuildUrl(HttpContext httpContext) |
|||
{ |
|||
//TODO: Add options to include/exclude query, schema and host
|
|||
|
|||
var uriBuilder = new UriBuilder(); |
|||
|
|||
uriBuilder.Scheme = httpContext.Request.Scheme; |
|||
uriBuilder.Host = httpContext.Request.Host.Host; |
|||
uriBuilder.Path = httpContext.Request.Path.ToString(); |
|||
uriBuilder.Query = httpContext.Request.QueryString.ToString(); |
|||
|
|||
return uriBuilder.Uri.AbsolutePath; |
|||
} |
|||
} |
|||
} |
|||
@ -1,60 +0,0 @@ |
|||
using System; |
|||
using Microsoft.AspNetCore.Http; |
|||
using Microsoft.Extensions.Logging; |
|||
using Microsoft.Extensions.Logging.Abstractions; |
|||
using Volo.Abp.Auditing; |
|||
|
|||
namespace Volo.Abp.AspNetCore.Auditing |
|||
{ |
|||
public class HttpContextClientInfoProvider : IClientInfoProvider |
|||
{ |
|||
public string BrowserInfo => GetBrowserInfo(); |
|||
|
|||
public string ClientIpAddress => GetClientIpAddress(); |
|||
|
|||
public string ComputerName => GetComputerName(); |
|||
|
|||
public ILogger<HttpContextClientInfoProvider> Logger { get; set; } |
|||
|
|||
private readonly IHttpContextAccessor _httpContextAccessor; |
|||
|
|||
private readonly HttpContext _httpContext; |
|||
|
|||
/// <summary>
|
|||
/// Creates a new <see cref="HttpContextClientInfoProvider"/>.
|
|||
/// </summary>
|
|||
public HttpContextClientInfoProvider(IHttpContextAccessor httpContextAccessor) |
|||
{ |
|||
_httpContextAccessor = httpContextAccessor; |
|||
_httpContext = httpContextAccessor.HttpContext; |
|||
|
|||
Logger = NullLogger<HttpContextClientInfoProvider>.Instance; |
|||
} |
|||
|
|||
protected virtual string GetBrowserInfo() |
|||
{ |
|||
var httpContext = _httpContextAccessor.HttpContext ?? _httpContext; |
|||
return httpContext?.Request?.Headers?["User-Agent"]; |
|||
} |
|||
|
|||
protected virtual string GetClientIpAddress() |
|||
{ |
|||
try |
|||
{ |
|||
var httpContext = _httpContextAccessor.HttpContext ?? _httpContext; |
|||
return httpContext?.Connection?.RemoteIpAddress?.ToString(); |
|||
} |
|||
catch (Exception ex) |
|||
{ |
|||
Logger.LogException(ex, LogLevel.Warning); |
|||
} |
|||
|
|||
return null; |
|||
} |
|||
|
|||
protected virtual string GetComputerName() |
|||
{ |
|||
return null; //TODO: Implement!
|
|||
} |
|||
} |
|||
} |
|||
@ -1,31 +0,0 @@ |
|||
using System; |
|||
using System.Threading.Tasks; |
|||
using Microsoft.Extensions.DependencyInjection; |
|||
|
|||
namespace Volo.Abp.Auditing |
|||
{ |
|||
public class ClientInfoAuditLogContributor : AuditLogContributor |
|||
{ |
|||
public override Task ContributeAsync(AuditLogContributionContext context) |
|||
{ |
|||
var clientInfoProvider = context.ServiceProvider.GetRequiredService<IClientInfoProvider>(); |
|||
|
|||
if (context.AuditInfo.ClientIpAddress.IsNullOrEmpty()) |
|||
{ |
|||
context.AuditInfo.ClientIpAddress = clientInfoProvider.ClientIpAddress; |
|||
} |
|||
|
|||
if (context.AuditInfo.BrowserInfo.IsNullOrEmpty()) |
|||
{ |
|||
context.AuditInfo.BrowserInfo = clientInfoProvider.BrowserInfo; |
|||
} |
|||
|
|||
if (context.AuditInfo.ClientName.IsNullOrEmpty()) |
|||
{ |
|||
context.AuditInfo.ClientName = clientInfoProvider.ComputerName; |
|||
} |
|||
|
|||
return Task.CompletedTask; |
|||
} |
|||
} |
|||
} |
|||
@ -1,11 +0,0 @@ |
|||
namespace Volo.Abp.Auditing |
|||
{ |
|||
public interface IClientInfoProvider |
|||
{ |
|||
string BrowserInfo { get; } |
|||
|
|||
string ClientIpAddress { get; } |
|||
|
|||
string ComputerName { get; } |
|||
} |
|||
} |
|||
@ -1,14 +0,0 @@ |
|||
using Volo.Abp.DependencyInjection; |
|||
|
|||
namespace Volo.Abp.Auditing |
|||
{ |
|||
[Dependency(TryRegister = true)] |
|||
public class NullClientInfoProvider : IClientInfoProvider, ISingletonDependency |
|||
{ |
|||
public string BrowserInfo => null; |
|||
|
|||
public string ClientIpAddress => null; |
|||
|
|||
public string ComputerName => null; |
|||
} |
|||
} |
|||
Loading…
Reference in new issue