57 lines
1.9 KiB
C#
57 lines
1.9 KiB
C#
namespace iFileProxy
|
|
{
|
|
using Serilog;
|
|
using Serilog.Events;
|
|
using System.Net;
|
|
|
|
public static class SerilogConfig
|
|
{
|
|
public static void CreateLogger()
|
|
{
|
|
var filePath = Path.Combine(AppContext.BaseDirectory, $"logs/dispatch.api.log");
|
|
|
|
Log.Logger = new LoggerConfiguration()
|
|
#if RELEASE
|
|
.MinimumLevel.Information()
|
|
#else
|
|
.MinimumLevel.Debug()
|
|
#endif
|
|
.MinimumLevel.Override("Microsoft", LogEventLevel.Information)
|
|
.MinimumLevel.Override("Microsoft.AspNetCore", LogEventLevel.Warning)
|
|
.Enrich.FromLogContext()
|
|
.WriteTo.Console(
|
|
outputTemplate: "{Timestamp:HH:mm:ss.fff} [{Level:u3}] [{SourceContext}] {ClientIp} {Message:lj} {contentType}{NewLine} {Exception}")
|
|
.WriteTo.File(filePath,
|
|
outputTemplate: "{Timestamp:yyyy-MM-dd HH:mm:ss.fff zzz} [{Level:u3}] [{SourceContext}] {ClientIp} {Message:lj} {contentType} {queryString}{NewLine}{Exception}",
|
|
rollingInterval: RollingInterval.Day,
|
|
fileSizeLimitBytes: 1073741824) //1GB
|
|
.Enrich.WithProperty("node_ip", GetIpAddress())
|
|
.CreateLogger();
|
|
}
|
|
|
|
public static void RefreshLogger()
|
|
{
|
|
if (Log.Logger != null)
|
|
{
|
|
Log.CloseAndFlush();
|
|
}
|
|
CreateLogger();
|
|
}
|
|
|
|
private static string GetIpAddress()
|
|
{
|
|
string ipAddress = "127.0.0.1";
|
|
IPAddress[] ips = Dns.GetHostAddresses(Dns.GetHostName());
|
|
foreach (IPAddress ip in ips)
|
|
{
|
|
if (ip.AddressFamily.ToString().ToLower().Equals("internetwork"))
|
|
{
|
|
ipAddress = ip.ToString();
|
|
return ipAddress;
|
|
}
|
|
}
|
|
|
|
return ipAddress;
|
|
}
|
|
}
|
|
}
|