2021年3月2日 星期二

Serial Log - Write to AWS Setting

            Log.Logger = new LoggerConfiguration()
                .Enrich.FromLogContext()
                .MinimumLevel.Debug()
                .MinimumLevel.Override("Microsoft", LogEventLevel.Warning)
                .Enrich.WithFunction("SystemUserName", () => Environment.UserName)
                .Enrich.WithFunction("SystemName", () => sysetmName)
                .Enrich.WithFunction("OSVersion", () => Environment.OSVersion.VersionString)
                .Enrich.WithFunction("CurrentManagedThreadId", () => Environment.CurrentManagedThreadId.ToString())
                .Enrich.WithFunction("CurrentTimeZone", () => TimeZone.CurrentTimeZone.StandardName)
                // 時間戳
                .Enrich.WithFunction("TimeStamp", () => {
                    System.DateTime startTime = TimeZone.CurrentTimeZone.ToLocalTime(new System.DateTime(1970, 1, 1)); // 當地時區
                    return ((long)(DateTime.Now - startTime).TotalSeconds).ToString(); // 相差秒數
                })
                .Enrich.WithFunction("LogGuid", () => Guid.NewGuid().ToString("N"))
                .WriteTo.AmazonS3(
                    new JsonFormatter(),
                    "log.log",
                    "logforservice",
                    Amazon.RegionEndpoint.APNortheast1,
                    "KeyId",
                    "Key",
                    fileSizeLimitBytes: 10,
                    autoUploadEvents: true,
                    rollingInterval: Serilog.Sinks.AmazonS3.RollingInterval.Minute,
                    bucketPath: $"{sysetmName}/{DateTime.Now.Year}/{DateTime.Now.Month}/{DateTime.Now.Day}/{DateTime.Now.Hour}"
                )
                .CreateLogger();

沒有留言:

張貼留言