/// <summary> /// Log request data /// </summary> /// <param name="context">Server call context</param> /// <param name="body">Request body expression</param> private void LogRequest(ServerCallContext context, string body) { if (_options.LogRequest) { body = Helpers.SecurityApplyBody(_options, "RPC", context.Method, body); AuditRequestInfo requestInfo = new AuditRequestInfo() { Id = _traceId, Scheme = _accessor.HttpContext.Request.Scheme, Headers = context.RequestHeaders.ToDictionary(k => k.Key, v => v.Value), Path = context.Method, Method = "RPC", Host = context.Host, QueryString = _accessor.HttpContext.Request.QueryString.Value, Body = body, ClientCertificate = _accessor.HttpContext.Connection.ClientCertificate?.SerialNumber, LocalIpAddress = _accessor.HttpContext.Connection.LocalIpAddress.ToString(), LocalPort = _accessor.HttpContext.Connection.LocalPort, RemoteIpAddress = _accessor.HttpContext.Connection.RemoteIpAddress.ToString(), RemotePort = _accessor.HttpContext.Connection.RemotePort }; _logger.Log(LogLevel.Information, _eventId, requestInfo, null, (i, e) => { return($"REQUEST: {requestInfo.Method} {requestInfo.RawUrl}"); }); } }
///<inheritdoc/> public void Log <TState> ( LogLevel logLevel, EventId eventId, TState state, Exception exception, Func <TState, Exception, string> formatter = null) { if ((this as ILogger).IsEnabled(logLevel)) { LogEntry info = new LogEntry() { Category = _category, Level = logLevel, Text = exception?.Message ?? state.ToString(), Exception = exception != null ? new LogExceptionInfo(exception) : null, EventId = eventId, ApplicationUser = GetSignedUserInformation() }; if (state is string) { info.Scopes.Add("Text", state.ToString()); } else if (state is AuditRequestInfo || state is AuditGrpcRequestInfo) { AuditRequestInfo auditRequest = state as AuditRequestInfo; info.Text = formatter?.Invoke(state, exception) ?? info.Text; if (auditRequest.Body != null && info.Text.Contains(auditRequest.Body)) { auditRequest.Body = null; } info.Scopes.Add("Date", auditRequest.Date.ToString("u")); info.Scopes.Add("Scheme", auditRequest.Scheme); if (auditRequest.Headers?.Count > 0) { foreach (var header in auditRequest.Headers) { info.Scopes.Add($"Headers.{header.Key}", header.Value.EscapeForJson()); //.ToEscapedString()); } } info.Scopes.Add("Method", auditRequest.Method); info.Scopes.Add("Host", auditRequest.Host); if (!string.IsNullOrWhiteSpace(auditRequest.QueryString)) { info.Scopes.Add("QueryString", auditRequest.QueryString); } info.Scopes.Add("ClientCertificate", auditRequest.ClientCertificate); info.Scopes.Add("LocalIpAddress", auditRequest.LocalIpAddress); info.Scopes.Add("LocalPort", auditRequest.LocalPort.ToString()); info.Scopes.Add("RemoteIpAddress", auditRequest.RemoteIpAddress); info.Scopes.Add("RemotePort", auditRequest.RemotePort.ToString()); info.Scopes.Add("RawUrl", auditRequest.RawUrl); if (!string.IsNullOrWhiteSpace(auditRequest.Body)) { if (state is AuditGrpcRequestInfo) { info.Scopes.Add("Body", auditRequest.Body); } else { info.Scopes.Add("Body", auditRequest.Body.AsJson()); } } } else if (state is AuditResponseInfo || state is AuditGrpcResponseInfo) { AuditResponseInfo auditResponse = state as AuditResponseInfo; info.Text = formatter?.Invoke(state, exception) ?? info.Text; if (auditResponse.Body != null && info.Text.Contains(auditResponse.Body)) { auditResponse.Body = null; } info.Scopes.Add("StatusCode", auditResponse.StatusCode.ToString()); info.Scopes.Add("Date", auditResponse.Date.ToString("u")); if (auditResponse.Headers?.Count > 0) { foreach (var header in auditResponse.Headers) { info.Scopes.Add($"Headers.{header.Key}", header.Value.EscapeForJson()); } } if (!string.IsNullOrWhiteSpace(auditResponse.Body)) { if (state is AuditGrpcResponseInfo) { info.Scopes.Add("Body", auditResponse.Body.ToString()); } else { info.Scopes.Add("Body", auditResponse.Body.AsJson()); } } } else if (state is IEnumerable <KeyValuePair <string, object> > properties) { info.Text = formatter?.Invoke(state, exception) ?? info.Text; foreach (KeyValuePair <string, object> item in properties) { if (item.Key != "{OriginalFormat}") { if (item.Value is string) { info.Scopes[item.Key] = item.Value.ToString(); } else if (item.Value?.GetType()?.Name == "RuntimeMethodInfo") { info.Scopes["MethodInfo.Module"] = (item.Value as MethodInfo).Module.Name; info.Scopes["MethodInfo.DeclaringType"] = (item.Value as MethodInfo).DeclaringType.Name; info.Scopes["MethodInfo.Name"] = (item.Value as MethodInfo).Name; } else { info.Scopes[item.Key] = item.Value.AsJson(); } } } } if (_provider.ScopeProvider != null) { _provider.ScopeProvider.ForEachScope((value, loggingProps) => { if (value is string) { info.Scopes["Text"] = value.ToString(); } else if (value is IEnumerable <KeyValuePair <string, object> > props) { foreach (KeyValuePair <string, object> pair in props) { string pairValue = null; if (pair.Value is string) { pairValue = pair.Value.ToString(); } else { pairValue = pair.Value.AsJson(); } if (!string.IsNullOrWhiteSpace(pairValue)) { info.Scopes[pair.Key] = pairValue; } } } }, state); info.Scopes["ApplicationName"] = Assembly.GetEntryAssembly().GetName().Name; info.Scopes["ApplicationVersion"] = Assembly.GetEntryAssembly().GetName().Version.ToString(); string environment = Environment.GetEnvironmentVariable("ASPNETCORE_ENVIRONMENT") ?? Environment.GetEnvironmentVariable("DOTNET_ENVIRONMENT"); info.Scopes["Environment"] = environment; } _provider.WriteLog(info); } }