You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
80 lines
3.1 KiB
80 lines
3.1 KiB
using Serilog;
|
|
using System.Collections.ObjectModel;
|
|
using System.Net;
|
|
using Serilog.Sinks.MSSqlServer;
|
|
|
|
namespace WMS_GIRAF_Interface
|
|
{
|
|
|
|
public static class SerilogConfig
|
|
{
|
|
public static void CreateLogger()
|
|
{
|
|
var filePath = Path.Combine(AppContext.BaseDirectory, "Logs/Serilog/log.txt");
|
|
#if DEBUG
|
|
var logDB = @"Server=JIAN_PC;database=CAN_Lucid_ASRS;uid=sa;pwd=Siasun;TrustServerCertificate=true";
|
|
#else
|
|
var logDB = @"Server=LUCID-ASRS-A01;database=CAN_Lucid_ASRS;uid=sa;pwd=Siasun123;TrustServerCertificate=true";
|
|
#endif
|
|
var sinkOpts = new MSSqlServerSinkOptions();
|
|
sinkOpts.TableName = "Logs_Serilog";
|
|
sinkOpts.AutoCreateSqlTable = true;
|
|
sinkOpts.BatchPostingLimit = 1;
|
|
sinkOpts.BatchPeriod = TimeSpan.FromSeconds(1);
|
|
var columnOpts = new ColumnOptions();
|
|
//columnOpts.Store.Remove(StandardColumn.Properties);
|
|
//columnOpts.Store.Add(StandardColumn.LogEvent);
|
|
//columnOpts.LogEvent.DataLength = 2048;
|
|
//columnOpts.PrimaryKey = columnOpts.TimeStamp;
|
|
//columnOpts.TimeStamp.NonClusteredIndex = true;
|
|
columnOpts.Store.Remove(StandardColumn.MessageTemplate);
|
|
columnOpts.Properties.ExcludeAdditionalProperties = true;
|
|
columnOpts.AdditionalColumns = new Collection<SqlColumn>
|
|
{
|
|
new SqlColumn{DataType = System.Data.SqlDbType.NVarChar, DataLength = 32, ColumnName = "IP"}
|
|
};
|
|
|
|
Log.Logger = new LoggerConfiguration()
|
|
.MinimumLevel.Information()
|
|
.WriteTo.Console(
|
|
outputTemplate: "{Timestamp:yyyy-MM-dd HH:mm:ss.fff zzz} [{Level:u3}] ({ThreadId}) {Message}{NewLine}{Exception}")
|
|
.WriteTo.File(filePath,
|
|
outputTemplate: "{Timestamp:yyyy-MM-dd HH:mm:ss.fff zzz} [{Level:u3}] ({ThreadId}) {Message:lj}{NewLine}{Exception}",
|
|
rollingInterval: RollingInterval.Day,
|
|
fileSizeLimitBytes: 1073741824).MinimumLevel.Information() //1GB
|
|
.Enrich.FromLogContext()
|
|
.Enrich.WithProperty("IP", GetIpAddress())
|
|
.WriteTo.MSSqlServer(
|
|
connectionString: logDB,
|
|
sinkOptions: sinkOpts,
|
|
columnOptions: columnOpts
|
|
).MinimumLevel.Warning().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;
|
|
}
|
|
}
|
|
|
|
}
|