From 18116eb4bd8a6a5158087f5c66e2a0a8d8deadc3 Mon Sep 17 00:00:00 2001 From: lu-xiaoshuang <121755080+lu-xiaoshuang@users.noreply.github.com> Date: Wed, 20 Mar 2024 08:00:00 +0800 Subject: [PATCH] enable logging config --- .../Config/InstrumentConfig.cs | 2 + .../Extensions/ServiceCollectionExtensions.cs | 25 +++++++++++- .../ConfigurationBuilderExtensions.cs | 39 +++++++++++-------- .../DefaultLoggerFactory.cs | 26 ++++++++++--- src/SkyApm.Utilities.Logging/LoggingConfig.cs | 20 +++++++++- 5 files changed, 87 insertions(+), 25 deletions(-) diff --git a/src/SkyApm.Abstractions/Config/InstrumentConfig.cs b/src/SkyApm.Abstractions/Config/InstrumentConfig.cs index acbc79e2..9a17dd5c 100644 --- a/src/SkyApm.Abstractions/Config/InstrumentConfig.cs +++ b/src/SkyApm.Abstractions/Config/InstrumentConfig.cs @@ -23,6 +23,8 @@ namespace SkyApm.Config [Config("SkyWalking")] public class InstrumentConfig { + public string Enable { get; set; } + public string Namespace { get; set; } [Obsolete("Use ServiceName.")] diff --git a/src/SkyApm.Agent.Hosting/Extensions/ServiceCollectionExtensions.cs b/src/SkyApm.Agent.Hosting/Extensions/ServiceCollectionExtensions.cs index 1f9fece1..dc9f1643 100644 --- a/src/SkyApm.Agent.Hosting/Extensions/ServiceCollectionExtensions.cs +++ b/src/SkyApm.Agent.Hosting/Extensions/ServiceCollectionExtensions.cs @@ -16,6 +16,8 @@ * */ +using System; +using Microsoft.Extensions.Configuration; using Microsoft.Extensions.Hosting; using SkyApm.Config; using SkyApm.Diagnostics; @@ -32,7 +34,6 @@ using SkyApm.Utilities.Configuration; using SkyApm.Utilities.DependencyInjection; using SkyApm.Utilities.Logging; -using System; using SkyApm; using SkyApm.Agent.Hosting; using SkyApm.Diagnostics.MSLogging; @@ -46,10 +47,30 @@ public static class ServiceCollectionExtensions { public static IServiceCollection AddSkyAPM(this IServiceCollection services, Action extensionsSetup = null) { + string environment = Environment.GetEnvironmentVariable("ASPNETCORE_ENVIRONMENT"); + if (environment == null || environment.Length < 1) + { + environment = "Development"; + } + IConfigurationBuilder configurationBuilder = new ConfigurationBuilder(); + configurationBuilder.AddJsonFile("skyapm.json", true); + configurationBuilder.AddJsonFile("skyapm." + environment + ".json", true); + IConfiguration configuration = configurationBuilder.Build(); + string enable = configuration?.GetSection("SkyWalking:Enable").Value ?? "false"; + if (null == enable || !"true".Equals(enable.ToLower())) + { + return services; + } + string serviceName = configuration?.GetSection("SkyWalking:ServiceName").Value ?? ""; + if (null == serviceName || serviceName.Length < 1) + { + return services; + } + Environment.SetEnvironmentVariable("SKYWALKING__SERVICENAME", serviceName); services.AddSkyAPMCore(extensionsSetup); return services; } - + private static IServiceCollection AddSkyAPMCore(this IServiceCollection services, Action extensionsSetup = null) { if (services == null) diff --git a/src/SkyApm.Utilities.Configuration/ConfigurationBuilderExtensions.cs b/src/SkyApm.Utilities.Configuration/ConfigurationBuilderExtensions.cs index 2fd3e7b6..07e3f555 100644 --- a/src/SkyApm.Utilities.Configuration/ConfigurationBuilderExtensions.cs +++ b/src/SkyApm.Utilities.Configuration/ConfigurationBuilderExtensions.cs @@ -34,22 +34,29 @@ public static IConfigurationBuilder AddSkyWalkingDefaultConfig(this IConfigurati var defaultLogFile = Path.Combine("logs", "skyapm-{Date}.log"); var defaultConfig = new Dictionary { - {"SkyWalking:Namespace", configuration?.GetSection("SkyWalking:Namespace").Value ?? string.Empty }, - {"SkyWalking:ServiceName", configuration?.GetSection("SkyWalking:ServiceName").Value ?? "My_Service" }, - {"Skywalking:ServiceInstanceName", configuration?.GetSection("SkyWalking:ServiceInstanceName").Value ?? BuildDefaultServiceInstanceName() }, - {"SkyWalking:HeaderVersions:0", configuration?.GetSection("SkyWalking:HeaderVersions:0").Value ?? HeaderVersions.SW8 }, - {"SkyWalking:Sampling:SamplePer3Secs", configuration?.GetSection("SkyWalking:Sampling:SamplePer3Secs").Value ?? "-1" }, - {"SkyWalking:Sampling:Percentage", configuration?.GetSection("SkyWalking:Sampling:Percentage").Value ?? "-1" }, - {"SkyWalking:Logging:Level", configuration?.GetSection("SkyWalking:Logging:Level").Value ?? "Information" }, - {"SkyWalking:Logging:FilePath", configuration?.GetSection("SkyWalking:Logging:FilePath").Value ?? defaultLogFile }, - {"SkyWalking:Transport:Interval", configuration?.GetSection("SkyWalking:Transport:Interval").Value ?? "3000" }, - {"SkyWalking:Transport:ProtocolVersion", configuration?.GetSection("SkyWalking:Transport:ProtocolVersion").Value ?? ProtocolVersions.V8 }, - {"SkyWalking:Transport:QueueSize", configuration?.GetSection("SkyWalking:Transport:QueueSize").Value ?? "30000" }, - {"SkyWalking:Transport:BatchSize", configuration?.GetSection("SkyWalking:Transport:BatchSize").Value ?? "3000" }, - {"SkyWalking:Transport:gRPC:Servers",configuration?.GetSection("SkyWalking:Transport:gRPC:Servers").Value ?? "localhost:11800" }, - {"SkyWalking:Transport:gRPC:Timeout",configuration?.GetSection("SkyWalking:Transport:gRPC:Timeout").Value ?? "10000" }, - {"SkyWalking:Transport:gRPC:ReportTimeout",configuration?.GetSection("SkyWalking:Transport:gRPC:ReportTimeout").Value ?? "600000" }, - {"SkyWalking:Transport:gRPC:ConnectTimeout",configuration?.GetSection("SkyWalking:Transport:gRPC:ConnectTimeout").Value ?? "10000" } + { "SkyWalking:Enable", configuration?.GetSection("SkyWalking:Enable").Value ?? "true" }, + { "SkyWalking:Namespace", configuration?.GetSection("SkyWalking:Namespace").Value ?? string.Empty }, + { "SkyWalking:ServiceName", configuration?.GetSection("SkyWalking:ServiceName").Value ?? "My_Service" }, + { "Skywalking:ServiceInstanceName", configuration?.GetSection("SkyWalking:ServiceInstanceName").Value ?? BuildDefaultServiceInstanceName() }, + { "SkyWalking:HeaderVersions:0", configuration?.GetSection("SkyWalking:HeaderVersions:0").Value ?? HeaderVersions.SW8 }, + { "SkyWalking:Sampling:SamplePer3Secs", configuration?.GetSection("SkyWalking:Sampling:SamplePer3Secs").Value ?? "-1" }, + { "SkyWalking:Sampling:Percentage", configuration?.GetSection("SkyWalking:Sampling:Percentage").Value ?? "-1" }, + { "SkyWalking:Logging:Level", configuration?.GetSection("SkyWalking:Logging:Level").Value ?? "Information" }, + { "SkyWalking:Logging:FilePath", configuration?.GetSection("SkyWalking:Logging:FilePath").Value ?? defaultLogFile }, + { "SkyWalking:Logging:FileSizeLimitBytes", configuration?.GetSection("SkyWalking:Logging:FileSizeLimitBytes").Value ?? "268435456" }, + { "SkyWalking:Logging:FlushToDiskInterval", configuration?.GetSection("SkyWalking:Logging:FlushToDiskInterval").Value ?? "1000" }, + { "SkyWalking:Logging:RollingInterval", configuration?.GetSection("SkyWalking:Logging:RollingInterval").Value ?? "Day" }, + { "SkyWalking:Logging:RollOnFileSizeLimit", configuration?.GetSection("SkyWalking:Logging:RollOnFileSizeLimit").Value ?? "false" }, + { "SkyWalking:Logging:RetainedFileCountLimit", configuration?.GetSection("SkyWalking:Logging:RetainedFileCountLimit").Value ?? "10" }, + { "SkyWalking:Logging:RetainedFileTimeLimit", configuration?.GetSection("SkyWalking:Logging:RetainedFileTimeLimit").Value ?? "864000000" }, + { "SkyWalking:Transport:Interval", configuration?.GetSection("SkyWalking:Transport:Interval").Value ?? "3000" }, + { "SkyWalking:Transport:ProtocolVersion", configuration?.GetSection("SkyWalking:Transport:ProtocolVersion").Value ?? ProtocolVersions.V8 }, + { "SkyWalking:Transport:QueueSize", configuration?.GetSection("SkyWalking:Transport:QueueSize").Value ?? "30000" }, + { "SkyWalking:Transport:BatchSize", configuration?.GetSection("SkyWalking:Transport:BatchSize").Value ?? "3000" }, + { "SkyWalking:Transport:gRPC:Servers", configuration?.GetSection("SkyWalking:Transport:gRPC:Servers").Value ?? "localhost:11800" }, + { "SkyWalking:Transport:gRPC:Timeout", configuration?.GetSection("SkyWalking:Transport:gRPC:Timeout").Value ?? "10000" }, + { "SkyWalking:Transport:gRPC:ReportTimeout", configuration?.GetSection("SkyWalking:Transport:gRPC:ReportTimeout").Value ?? "600000" }, + { "SkyWalking:Transport:gRPC:ConnectTimeout", configuration?.GetSection("SkyWalking:Transport:gRPC:ConnectTimeout").Value ?? "10000" } }; return builder.AddInMemoryCollection(defaultConfig); } diff --git a/src/SkyApm.Utilities.Logging/DefaultLoggerFactory.cs b/src/SkyApm.Utilities.Logging/DefaultLoggerFactory.cs index 73586266..e463e16e 100644 --- a/src/SkyApm.Utilities.Logging/DefaultLoggerFactory.cs +++ b/src/SkyApm.Utilities.Logging/DefaultLoggerFactory.cs @@ -41,15 +41,29 @@ public DefaultLoggerFactory(IConfigAccessor configAccessor) _loggerFactory = new MSLoggerFactory(); var instrumentationConfig = configAccessor.Get(); - var level = EventLevel(_loggingConfig.Level); + var __level = EventLevel(_loggingConfig.Level); + long __fileSizeLimitBytes = _loggingConfig.FileSizeLimitBytes ?? 1024 * 1024 * 256; + long __flushToDiskInterval = _loggingConfig.FlushToDiskInterval ?? 1000; + string __rollingInterval = _loggingConfig.RollingInterval ?? "Day"; + bool __rollOnFileSizeLimit = _loggingConfig.RollOnFileSizeLimit ?? false; + int __retainedFileCountLimit = _loggingConfig.RetainedFileCountLimit ?? 10; + long __retainedFileTimeLimit = _loggingConfig.RetainedFileTimeLimit ?? 1000 * 60 * 60 * 24 * 10; _loggerFactory.AddSerilog(new LoggerConfiguration().MinimumLevel.Verbose().Enrich .WithProperty("SourceContext", null).Enrich - .WithProperty(nameof(instrumentationConfig.ServiceName), - instrumentationConfig.ServiceName).Enrich - //_loggingConfig.FilePath, level, outputTemplate, null, 1073741824,31, null, false, false, TimeSpan.FromMilliseconds(500) - .FromLogContext().WriteTo.Async(o => - o.File(_loggingConfig.FilePath, level, outputTemplate, flushToDiskInterval: TimeSpan.FromMilliseconds(500), rollingInterval: RollingInterval.Month)) + .WithProperty(nameof(instrumentationConfig.ServiceName), instrumentationConfig.ServiceName).Enrich + .FromLogContext() + .WriteTo + .Async(o => o.File( + _loggingConfig.FilePath, + __level, + outputTemplate, + fileSizeLimitBytes: __fileSizeLimitBytes, + flushToDiskInterval: TimeSpan.FromMilliseconds(__flushToDiskInterval), + rollingInterval: (RollingInterval)(Enum.Parse(typeof(RollingInterval), __rollingInterval)), + rollOnFileSizeLimit: __rollOnFileSizeLimit, + retainedFileCountLimit: __retainedFileCountLimit, + retainedFileTimeLimit: TimeSpan.FromMilliseconds(__retainedFileTimeLimit))) .CreateLogger()); } diff --git a/src/SkyApm.Utilities.Logging/LoggingConfig.cs b/src/SkyApm.Utilities.Logging/LoggingConfig.cs index 45947275..6d1697ab 100644 --- a/src/SkyApm.Utilities.Logging/LoggingConfig.cs +++ b/src/SkyApm.Utilities.Logging/LoggingConfig.cs @@ -26,5 +26,23 @@ public class LoggingConfig public string Level { get; set; } public string FilePath { get; set; } + + public long? FileSizeLimitBytes { get; set; } + + /// + /// in milliseconds + /// + public long? FlushToDiskInterval { get; set; } + + public string RollingInterval { get; set; } + + public bool? RollOnFileSizeLimit { get; set; } + + public int? RetainedFileCountLimit { get; set; } + + /// + /// in milliseconds + /// + public long? RetainedFileTimeLimit { get; set; } } -} \ No newline at end of file +}