2021年3月2日 星期二

Serial Log - Write to AWS Setting

  1. Log.Logger = new LoggerConfiguration()
  2. .Enrich.FromLogContext()
  3. .MinimumLevel.Debug()
  4. .MinimumLevel.Override("Microsoft", LogEventLevel.Warning)
  5. .Enrich.WithFunction("SystemUserName", () => Environment.UserName)
  6. .Enrich.WithFunction("SystemName", () => sysetmName)
  7. .Enrich.WithFunction("OSVersion", () => Environment.OSVersion.VersionString)
  8. .Enrich.WithFunction("CurrentManagedThreadId", () => Environment.CurrentManagedThreadId.ToString())
  9. .Enrich.WithFunction("CurrentTimeZone", () => TimeZone.CurrentTimeZone.StandardName)
  10. // 時間戳
  11. .Enrich.WithFunction("TimeStamp", () => {
  12. System.DateTime startTime = TimeZone.CurrentTimeZone.ToLocalTime(new System.DateTime(1970, 1, 1)); // 當地時區
  13. return ((long)(DateTime.Now - startTime).TotalSeconds).ToString(); // 相差秒數
  14. })
  15. .Enrich.WithFunction("LogGuid", () => Guid.NewGuid().ToString("N"))
  16. .WriteTo.AmazonS3(
  17. new JsonFormatter(),
  18. "log.log",
  19. "logforservice",
  20. Amazon.RegionEndpoint.APNortheast1,
  21. "KeyId",
  22. "Key",
  23. fileSizeLimitBytes: 10,
  24. autoUploadEvents: true,
  25. rollingInterval: Serilog.Sinks.AmazonS3.RollingInterval.Minute,
  26. bucketPath: $"{sysetmName}/{DateTime.Now.Year}/{DateTime.Now.Month}/{DateTime.Now.Day}/{DateTime.Now.Hour}"
  27. )
  28. .CreateLogger();
  29.  

沒有留言:

張貼留言