Skip to content

Commit

Permalink
enable logging config
Browse files Browse the repository at this point in the history
  • Loading branch information
lu-xiaoshuang authored and luxiaoshuang committed Mar 22, 2024
1 parent 9dc9cc0 commit 18116eb
Show file tree
Hide file tree
Showing 5 changed files with 87 additions and 25 deletions.
2 changes: 2 additions & 0 deletions src/SkyApm.Abstractions/Config/InstrumentConfig.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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.")]
Expand Down
25 changes: 23 additions & 2 deletions src/SkyApm.Agent.Hosting/Extensions/ServiceCollectionExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@
*
*/

using System;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.Hosting;
using SkyApm.Config;
using SkyApm.Diagnostics;
Expand All @@ -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;
Expand All @@ -46,10 +47,30 @@ public static class ServiceCollectionExtensions
{
public static IServiceCollection AddSkyAPM(this IServiceCollection services, Action<SkyApmExtensions> 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<SkyApmExtensions> extensionsSetup = null)
{
if (services == null)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,22 +34,29 @@ public static IConfigurationBuilder AddSkyWalkingDefaultConfig(this IConfigurati
var defaultLogFile = Path.Combine("logs", "skyapm-{Date}.log");
var defaultConfig = new Dictionary<string, string>
{
{"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);
}
Expand Down
26 changes: 20 additions & 6 deletions src/SkyApm.Utilities.Logging/DefaultLoggerFactory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -41,15 +41,29 @@ public DefaultLoggerFactory(IConfigAccessor configAccessor)
_loggerFactory = new MSLoggerFactory();
var instrumentationConfig = configAccessor.Get<InstrumentConfig>();

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());
}

Expand Down
20 changes: 19 additions & 1 deletion src/SkyApm.Utilities.Logging/LoggingConfig.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,5 +26,23 @@ public class LoggingConfig
public string Level { get; set; }

public string FilePath { get; set; }

public long? FileSizeLimitBytes { get; set; }

/// <summary>
/// in milliseconds
/// </summary>
public long? FlushToDiskInterval { get; set; }

public string RollingInterval { get; set; }

public bool? RollOnFileSizeLimit { get; set; }

public int? RetainedFileCountLimit { get; set; }

/// <summary>
/// in milliseconds
/// </summary>
public long? RetainedFileTimeLimit { get; set; }
}
}
}

0 comments on commit 18116eb

Please sign in to comment.