Skip to content

Commit

Permalink
run dotnet-format
Browse files Browse the repository at this point in the history
  • Loading branch information
Mpdreamz committed Jun 3, 2024
1 parent 734172a commit 65ee18a
Show file tree
Hide file tree
Showing 5 changed files with 83 additions and 80 deletions.
127 changes: 64 additions & 63 deletions src/Elastic.Apm/Logging/GlobalLogConfiguration.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,79 +26,80 @@ internal class EnvironmentLoggingConfiguration(IDictionary environmentVariables
{
public IDictionary EnvironmentVariables { get; } = environmentVariables ?? Environment.GetEnvironmentVariables();

public string GetSafeEnvironmentVariable(string key)
{
var value = EnvironmentVariables.Contains(key) ? EnvironmentVariables[key]?.ToString() : null;
return value ?? string.Empty;
}
public string GetSafeEnvironmentVariable(string key)
{
var value = EnvironmentVariables.Contains(key) ? EnvironmentVariables[key]?.ToString() : null;
return value ?? string.Empty;
}

public LogLevel GetLogLevel(params string[] keys)
{
var level = keys
.Select(k => GetSafeEnvironmentVariable(k))
.Select<string, LogLevel?>(v => v.ToLowerInvariant() switch
{
"trace" => LogLevel.Trace,
"debug" => LogLevel.Debug,
"info" => LogLevel.Information,
"warn" => LogLevel.Warning,
"error" => LogLevel.Error,
"none" => LogLevel.None,
_ => null
})
.FirstOrDefault(l => l != null);
return level ?? LogLevel.Warning;
}
public LogLevel GetLogLevel(params string[] keys)
{
var level = keys
.Select(k => GetSafeEnvironmentVariable(k))
.Select<string, LogLevel?>(v => v.ToLowerInvariant() switch
{
"trace" => LogLevel.Trace,
"debug" => LogLevel.Debug,
"info" => LogLevel.Information,
"warn" => LogLevel.Warning,
"error" => LogLevel.Error,
"none" => LogLevel.None,
_ => null
})
.FirstOrDefault(l => l != null);
return level ?? LogLevel.Warning;
}

public string GetLogFilePath(params string[] keys)
{
var path = keys
.Select(k => GetSafeEnvironmentVariable(k))
.FirstOrDefault(p => !string.IsNullOrEmpty(p));
public string GetLogFilePath(params string[] keys)
{
var path = keys
.Select(k => GetSafeEnvironmentVariable(k))
.FirstOrDefault(p => !string.IsNullOrEmpty(p));

return path ?? GetDefaultLogDirectory();
}
return path ?? GetDefaultLogDirectory();
}

public bool AnyConfigured(params string[] keys) =>
keys
.Select(k => GetSafeEnvironmentVariable(k))
.Any(p => !string.IsNullOrEmpty(p));
public bool AnyConfigured(params string[] keys) =>
keys
.Select(k => GetSafeEnvironmentVariable(k))
.Any(p => !string.IsNullOrEmpty(p));

public GlobalLogTarget ParseLogTargets(params string[] keys)
{
public GlobalLogTarget ParseLogTargets(params string[] keys)
{

var targets = keys
.Select(k => GetSafeEnvironmentVariable(k))
.FirstOrDefault(p => !string.IsNullOrEmpty(p));
if (string.IsNullOrWhiteSpace(targets))
return GlobalLogTarget.File;
var targets = keys
.Select(k => GetSafeEnvironmentVariable(k))
.FirstOrDefault(p => !string.IsNullOrEmpty(p));
if (string.IsNullOrWhiteSpace(targets))
return GlobalLogTarget.File;

var logTargets = GlobalLogTarget.None;
var found = false;
var logTargets = GlobalLogTarget.None;
var found = false;

foreach (var target in targets.Split(new [] {';'}, StringSplitOptions.RemoveEmptyEntries))
{
if (IsSet(target, "stdout"))
logTargets |= GlobalLogTarget.StdOut;
else if (IsSet(target, "file"))
logTargets |= GlobalLogTarget.File;
else if (IsSet(target, "none"))
logTargets |= GlobalLogTarget.None;
}
return !found ? GlobalLogTarget.File : logTargets;

bool IsSet(string k, string v)
{
var b = k.Trim().Equals(v, StringComparison.InvariantCultureIgnoreCase);
if (b) found = true;
return b;
}
foreach (var target in targets.Split(new[] { ';' }, StringSplitOptions.RemoveEmptyEntries))
{
if (IsSet(target, "stdout"))
logTargets |= GlobalLogTarget.StdOut;
else if (IsSet(target, "file"))
logTargets |= GlobalLogTarget.File;
else if (IsSet(target, "none"))
logTargets |= GlobalLogTarget.None;
}
return !found ? GlobalLogTarget.File : logTargets;

bool IsSet(string k, string v)
{
var b = k.Trim().Equals(v, StringComparison.InvariantCultureIgnoreCase);
if (b)
found = true;
return b;
}
}

internal static string GetDefaultLogDirectory() =>
Environment.OSVersion.Platform == PlatformID.Win32NT
? Path.Combine(Environment.GetEnvironmentVariable("PROGRAMDATA")!, "elastic", "apm-agent-dotnet", "logs")
: "/var/log/elastic/apm-agent-dotnet";
internal static string GetDefaultLogDirectory() =>
Environment.OSVersion.Platform == PlatformID.Win32NT
? Path.Combine(Environment.GetEnvironmentVariable("PROGRAMDATA")!, "elastic", "apm-agent-dotnet", "logs")
: "/var/log/elastic/apm-agent-dotnet";

}

Expand Down
26 changes: 13 additions & 13 deletions src/integrations/Elastic.Apm.Extensions.Hosting/CompositeLogger.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,25 +11,25 @@ namespace Elastic.Apm.Extensions.Hosting;
internal sealed class CompositeLogger(TraceLogger traceLogger, IApmLogger logger) : IDisposable , IApmLogger
{
public TraceLogger TraceLogger { get; } = traceLogger;
public IApmLogger ApmLogger { get; } = logger;
public IApmLogger ApmLogger { get; } = logger;

private bool _isDisposed;
private bool _isDisposed;

public void Dispose() => _isDisposed = true;
public void Dispose() => _isDisposed = true;

public void Log<TState>(LogLevel level, TState state, Exception e, Func<TState, Exception, string> formatter)
{
if (_isDisposed)
return;
public void Log<TState>(LogLevel level, TState state, Exception e, Func<TState, Exception, string> formatter)
{
if (_isDisposed)
return;

if (TraceLogger.IsEnabled(level))
TraceLogger.Log(level, state, e, formatter);
if (TraceLogger.IsEnabled(level))
TraceLogger.Log(level, state, e, formatter);

if (ApmLogger.IsEnabled(level))
ApmLogger.Log(level, state, e, formatter);
}
if (ApmLogger.IsEnabled(level))
ApmLogger.Log(level, state, e, formatter);
}

public bool IsEnabled(LogLevel logLevel) => ApmLogger.IsEnabled(logLevel) || TraceLogger.IsEnabled(logLevel);
public bool IsEnabled(LogLevel logLevel) => ApmLogger.IsEnabled(logLevel) || TraceLogger.IsEnabled(logLevel);


}
3 changes: 2 additions & 1 deletion src/profiler/Elastic.Apm.Profiler.Managed.Loader/Logger.cs
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,8 @@ public static void Log(LogLevel level, string message, params object[] args)
if (!IsActive || Level > level)
return;

if (string.IsNullOrWhiteSpace(LogFile)) return;
if (string.IsNullOrWhiteSpace(LogFile))
return;

try
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,8 @@ public void CheckLogTargetsPrecedence()
private static GlobalLogConfiguration CreateConfig(params (string key, string v)[] values)
{
var environment = new Hashtable();
foreach (var kv in values) environment.Add(kv.key, kv.v);
foreach (var kv in values)
environment.Add(kv.key, kv.v);
var config = GlobalLogConfiguration.FromEnvironment(environment);
return config;
}
Expand Down
4 changes: 2 additions & 2 deletions test/Elastic.Apm.Tests/Config/GlobalLogConfigurationTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ public void Check_Defaults()
[InlineData(ELASTIC_APM_PROFILER_LOG_TARGETS, "BadEnumValue")]
public void CheckActivation(string environmentVariable, string value)
{
var config = GlobalLogConfiguration.FromEnvironment(new Hashtable { { environmentVariable, value}});
var config = GlobalLogConfiguration.FromEnvironment(new Hashtable { { environmentVariable, value } });
config.IsActive.Should().BeTrue();
}

Expand All @@ -58,7 +58,7 @@ public void CheckActivation(string environmentVariable, string value)
[InlineData(ELASTIC_APM_PROFILER_LOG, "None")]
public void CheckNonActivation(string environmentVariable, string value)
{
var config = GlobalLogConfiguration.FromEnvironment(new Hashtable { { environmentVariable , value}});
var config = GlobalLogConfiguration.FromEnvironment(new Hashtable { { environmentVariable, value } });
config.IsActive.Should().BeFalse();
}

Expand Down

0 comments on commit 65ee18a

Please sign in to comment.