init
This commit is contained in:
commit
e124a47765
19374 changed files with 9806149 additions and 0 deletions
44
KretaWeb/Logger/HangfireLogger.cs
Normal file
44
KretaWeb/Logger/HangfireLogger.cs
Normal file
|
@ -0,0 +1,44 @@
|
|||
using System;
|
||||
using System.Configuration;
|
||||
using System.Diagnostics;
|
||||
using Serilog;
|
||||
using Serilog.Exceptions;
|
||||
using Serilog.Formatting.Elasticsearch;
|
||||
using Serilog.Sinks.Elasticsearch;
|
||||
using LogEventLevel = Serilog.Events.LogEventLevel;
|
||||
|
||||
namespace Kreta.Web.Logger
|
||||
{
|
||||
public static class HangfireLogger
|
||||
{
|
||||
public static void SerilogGlobalConfiguration()
|
||||
{
|
||||
#if DEBUG
|
||||
Serilog.Debugging.SelfLog.Enable(msg => Debug.WriteLine(msg));
|
||||
#endif
|
||||
|
||||
Enum.TryParse<LogEventLevel>(ConfigurationManager.AppSettings["serilog:minimum-level"], true, out LogEventLevel logEventLevel);
|
||||
var nodeUris = ConfigurationManager.AppSettings["serilog:write-to:Elasticsearch.nodeUris"];
|
||||
var indexFormat = ConfigurationManager.AppSettings["serilog:write-to:Elasticsearch.indexFormat"];
|
||||
bool.TryParse(ConfigurationManager.AppSettings["serilog:write-to:Elasticsearch.autoRegisterTemplate"], out var autoRegisterTemplate);
|
||||
Enum.TryParse<AutoRegisterTemplateVersion>(ConfigurationManager.AppSettings["serilog:write-to:Elasticsearch.autoRegisterTemplateVersion"], true, out AutoRegisterTemplateVersion autoRegisterTemplateVersion);
|
||||
|
||||
Log.Logger = new LoggerConfiguration()
|
||||
.Enrich.WithExceptionDetails()
|
||||
.WriteTo.Elasticsearch(new ElasticsearchSinkOptions(new Uri(nodeUris))
|
||||
{
|
||||
IndexFormat = indexFormat,
|
||||
MinimumLogEventLevel = logEventLevel,
|
||||
AutoRegisterTemplate = autoRegisterTemplate,
|
||||
AutoRegisterTemplateVersion = autoRegisterTemplateVersion,
|
||||
CustomFormatter = new ExceptionAsObjectJsonFormatter(renderMessage: true)
|
||||
})
|
||||
.CreateLogger();
|
||||
}
|
||||
|
||||
public static void ShutdownSerilog()
|
||||
{
|
||||
Log.CloseAndFlush();
|
||||
}
|
||||
}
|
||||
}
|
86
KretaWeb/Logger/LoginRequestBodyRenderer.cs
Normal file
86
KretaWeb/Logger/LoginRequestBodyRenderer.cs
Normal file
|
@ -0,0 +1,86 @@
|
|||
using System;
|
||||
using System.Text;
|
||||
using Kreta.Web.Logging.Logger;
|
||||
using Newtonsoft.Json;
|
||||
using Newtonsoft.Json.Linq;
|
||||
|
||||
namespace Kreta.Web.Logger
|
||||
{
|
||||
/// <summary>
|
||||
/// Logging field renderer
|
||||
/// </summary>
|
||||
class LoginRequestBodyRenderer : LoggingFieldRenderer
|
||||
{
|
||||
/// <summary>
|
||||
/// Secret log value
|
||||
/// </summary>
|
||||
const string SecretLogValue = "***";
|
||||
|
||||
/// <summary>
|
||||
/// Password
|
||||
/// </summary>
|
||||
const string Password = nameof(Password);
|
||||
|
||||
/// <summary>
|
||||
/// User
|
||||
/// </summary>
|
||||
const string UserName = nameof(UserName);
|
||||
|
||||
/// <summary>
|
||||
/// Render
|
||||
/// </summary>
|
||||
/// <param name="fieldName">Field</param>
|
||||
/// <param name="context">Context</param>
|
||||
public override void Render(string fieldName, LoggingFieldRendererContext context)
|
||||
{
|
||||
string contentBodyJsonText = context.GetFieldValue(fieldName) as string;
|
||||
|
||||
if (contentBodyJsonText != null)
|
||||
{
|
||||
try
|
||||
{
|
||||
JObject contentBody = JsonConvert.DeserializeObject<JObject>(contentBodyJsonText);
|
||||
|
||||
if (!string.IsNullOrWhiteSpace(contentBody.GetValue(Password, StringComparison.OrdinalIgnoreCase)?.ToString()))
|
||||
{
|
||||
contentBody[Password] = SecretLogValue;
|
||||
}
|
||||
|
||||
string userLoginName = contentBody.GetValue(UserName, StringComparison.OrdinalIgnoreCase)?.ToString();
|
||||
|
||||
StringBuilder requestUserIdentifier = new StringBuilder();
|
||||
if (userLoginName != null)
|
||||
{
|
||||
requestUserIdentifier.Append(userLoginName.ToUpper());
|
||||
|
||||
string requestUri = context.GetFieldValue(RequestResponseLoggingFields.Request.Uri) as string;
|
||||
|
||||
if (!string.IsNullOrWhiteSpace(requestUri))
|
||||
{
|
||||
requestUserIdentifier.Append('/');
|
||||
|
||||
try
|
||||
{
|
||||
Uri uri = new Uri(requestUri);
|
||||
requestUserIdentifier.Append(uri.Authority.Split('.')[0]);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
requestUserIdentifier.Append($"[{ex.Message}]");
|
||||
}
|
||||
}
|
||||
|
||||
context.SetFieldValue(RequestResponseLoggingFields.Request.User, requestUserIdentifier.ToString());
|
||||
}
|
||||
|
||||
context.SetFieldValue(fieldName, contentBody.ToString());
|
||||
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
context.SetFieldValue(fieldName, ex.ToString());
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
43
KretaWeb/Logger/RequestUserRenderer.cs
Normal file
43
KretaWeb/Logger/RequestUserRenderer.cs
Normal file
|
@ -0,0 +1,43 @@
|
|||
using System;
|
||||
using System.Collections.Generic;
|
||||
using Kreta.Web.Logging.Logger;
|
||||
using Kreta.Web.Security;
|
||||
|
||||
namespace Kreta.Web.Logger
|
||||
{
|
||||
/// <summary>
|
||||
/// Logging field renderer
|
||||
/// </summary>
|
||||
class RequestUserRenderer : LoggingFieldRenderer
|
||||
{
|
||||
/// <summary>
|
||||
/// Render
|
||||
/// </summary>
|
||||
/// <param name="fieldName">Field</param>
|
||||
/// <param name="context">Context</param>
|
||||
public override void Render(string fieldName, LoggingFieldRendererContext context)
|
||||
{
|
||||
if (string.Compare(RequestResponseLoggingFields.Request.User, fieldName, StringComparison.OrdinalIgnoreCase) != 0)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
string user = context.GetFieldValue(fieldName) as string;
|
||||
if (!string.IsNullOrWhiteSpace(user))
|
||||
{
|
||||
try
|
||||
{
|
||||
List<string> roles = ClaimData.FelhasznaloSzerepCsomagok;
|
||||
if (roles != null && roles.Count > 0)
|
||||
{
|
||||
context.SetFieldValue(fieldName, $"{user}/[{string.Join(",", roles)}]");
|
||||
}
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
context.SetFieldValue(fieldName, ex.ToString());
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue