add command to set log level and develop mode dynamically
This commit is contained in:
parent
1e88f5bac0
commit
4233aab1ee
80
Application/Commands/SetLogLevelCommand.cs
Normal file
80
Application/Commands/SetLogLevelCommand.cs
Normal file
@ -0,0 +1,80 @@
|
|||||||
|
using System;
|
||||||
|
using System.Threading.Tasks;
|
||||||
|
using Data.Models.Client;
|
||||||
|
using Serilog.Core;
|
||||||
|
using Serilog.Events;
|
||||||
|
using SharedLibraryCore;
|
||||||
|
using SharedLibraryCore.Commands;
|
||||||
|
using SharedLibraryCore.Configuration;
|
||||||
|
using SharedLibraryCore.Interfaces;
|
||||||
|
|
||||||
|
namespace IW4MAdmin.Application.Commands;
|
||||||
|
|
||||||
|
public class SetLogLevelCommand : Command
|
||||||
|
{
|
||||||
|
private readonly Func<string, LoggingLevelSwitch> _levelSwitchResolver;
|
||||||
|
|
||||||
|
public SetLogLevelCommand(CommandConfiguration config, ITranslationLookup layout, Func<string, LoggingLevelSwitch> levelSwitchResolver) : base(config, layout)
|
||||||
|
{
|
||||||
|
_levelSwitchResolver = levelSwitchResolver;
|
||||||
|
|
||||||
|
Name = "loglevel";
|
||||||
|
Alias = "ll";
|
||||||
|
Description = "set minimum logging level";
|
||||||
|
Permission = EFClient.Permission.Owner;
|
||||||
|
Arguments = new CommandArgument[]
|
||||||
|
{
|
||||||
|
new()
|
||||||
|
{
|
||||||
|
Name = "Log Level",
|
||||||
|
Required = true
|
||||||
|
},
|
||||||
|
new()
|
||||||
|
{
|
||||||
|
Name = "Override",
|
||||||
|
Required = false
|
||||||
|
},
|
||||||
|
new()
|
||||||
|
{
|
||||||
|
Name = "IsDevelopment",
|
||||||
|
Required = false
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
public override async Task ExecuteAsync(GameEvent gameEvent)
|
||||||
|
{
|
||||||
|
var args = gameEvent.Data.Split(" ");
|
||||||
|
if (!Enum.TryParse<LogEventLevel>(args[0], out var minLevel))
|
||||||
|
{
|
||||||
|
await gameEvent.Origin.TellAsync(new[]
|
||||||
|
{
|
||||||
|
$"Valid log values: {string.Join(",", Enum.GetValues<LogEventLevel>())}"
|
||||||
|
});
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
var context = string.Empty;
|
||||||
|
|
||||||
|
if (args.Length > 1)
|
||||||
|
{
|
||||||
|
context = args[1];
|
||||||
|
}
|
||||||
|
|
||||||
|
var loggingSwitch = _levelSwitchResolver(context);
|
||||||
|
loggingSwitch.MinimumLevel = minLevel;
|
||||||
|
|
||||||
|
if (args.Length > 2 && (args[2] == "1" || args[2].ToLower() == "true"))
|
||||||
|
{
|
||||||
|
AppContext.SetSwitch("IsDevelop", true);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
AppContext.SetSwitch("IsDevelop", false);
|
||||||
|
}
|
||||||
|
|
||||||
|
await gameEvent.Origin.TellAsync(new[]
|
||||||
|
{ $"Set minimum log level to {loggingSwitch.MinimumLevel.ToString()}" });
|
||||||
|
}
|
||||||
|
}
|
@ -8,6 +8,7 @@ using Microsoft.Extensions.Configuration;
|
|||||||
using Microsoft.Extensions.DependencyInjection;
|
using Microsoft.Extensions.DependencyInjection;
|
||||||
using Microsoft.Extensions.Logging;
|
using Microsoft.Extensions.Logging;
|
||||||
using Serilog;
|
using Serilog;
|
||||||
|
using Serilog.Core;
|
||||||
using Serilog.Events;
|
using Serilog.Events;
|
||||||
using SharedLibraryCore;
|
using SharedLibraryCore;
|
||||||
using SharedLibraryCore.Configuration;
|
using SharedLibraryCore.Configuration;
|
||||||
@ -17,7 +18,10 @@ namespace IW4MAdmin.Application.Extensions
|
|||||||
{
|
{
|
||||||
public static class StartupExtensions
|
public static class StartupExtensions
|
||||||
{
|
{
|
||||||
private static ILogger _defaultLogger = null;
|
private static ILogger _defaultLogger;
|
||||||
|
private static readonly LoggingLevelSwitch LevelSwitch = new();
|
||||||
|
private static readonly LoggingLevelSwitch MicrosoftLevelSwitch = new();
|
||||||
|
private static readonly LoggingLevelSwitch SystemLevelSwitch = new();
|
||||||
|
|
||||||
public static IServiceCollection AddBaseLogger(this IServiceCollection services,
|
public static IServiceCollection AddBaseLogger(this IServiceCollection services,
|
||||||
ApplicationConfiguration appConfig)
|
ApplicationConfiguration appConfig)
|
||||||
@ -29,21 +33,37 @@ namespace IW4MAdmin.Application.Extensions
|
|||||||
.Build();
|
.Build();
|
||||||
|
|
||||||
var loggerConfig = new LoggerConfiguration()
|
var loggerConfig = new LoggerConfiguration()
|
||||||
.ReadFrom.Configuration(configuration)
|
.ReadFrom.Configuration(configuration);
|
||||||
.MinimumLevel.Override("Microsoft", LogEventLevel.Warning);
|
|
||||||
|
LevelSwitch.MinimumLevel = Enum.Parse<LogEventLevel>(configuration["Serilog:MinimumLevel:Default"]);
|
||||||
|
MicrosoftLevelSwitch.MinimumLevel = Enum.Parse<LogEventLevel>(configuration["Serilog:MinimumLevel:Override:Microsoft"]);
|
||||||
|
SystemLevelSwitch.MinimumLevel = Enum.Parse<LogEventLevel>(configuration["Serilog:MinimumLevel:Override:System"]);
|
||||||
|
|
||||||
|
loggerConfig = loggerConfig.MinimumLevel.ControlledBy(LevelSwitch);
|
||||||
|
loggerConfig = loggerConfig.MinimumLevel.Override("Microsoft", MicrosoftLevelSwitch)
|
||||||
|
.MinimumLevel.Override("System", SystemLevelSwitch);
|
||||||
|
|
||||||
if (Utilities.IsDevelopment)
|
if (Utilities.IsDevelopment)
|
||||||
{
|
{
|
||||||
loggerConfig = loggerConfig.WriteTo.Console(
|
loggerConfig = loggerConfig.WriteTo.Console(
|
||||||
outputTemplate:
|
outputTemplate:
|
||||||
"[{Timestamp:yyyy-MM-dd HH:mm:ss.fff} {Server} {Level:u3}] {Message:lj}{NewLine}{Exception}")
|
"[{Timestamp:HH:mm:ss} {Server} {Level:u3}] {Message:lj}{NewLine}{Exception}")
|
||||||
.MinimumLevel.Override("Microsoft", LogEventLevel.Information)
|
; //.MinimumLevel.Override("Microsoft", LogEventLevel.Information)
|
||||||
.MinimumLevel.Debug();
|
//.MinimumLevel.Debug();
|
||||||
}
|
}
|
||||||
|
|
||||||
_defaultLogger = loggerConfig.CreateLogger();
|
_defaultLogger = loggerConfig.CreateLogger();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
services.AddSingleton((string context) =>
|
||||||
|
{
|
||||||
|
return context.ToLower() switch
|
||||||
|
{
|
||||||
|
"microsoft" => MicrosoftLevelSwitch,
|
||||||
|
"system" => SystemLevelSwitch,
|
||||||
|
_ => LevelSwitch
|
||||||
|
};
|
||||||
|
});
|
||||||
services.AddLogging(builder => builder.AddSerilog(_defaultLogger, dispose: true));
|
services.AddLogging(builder => builder.AddSerilog(_defaultLogger, dispose: true));
|
||||||
services.AddSingleton(new LoggerFactory()
|
services.AddSingleton(new LoggerFactory()
|
||||||
.AddSerilog(_defaultLogger, true));
|
.AddSerilog(_defaultLogger, true));
|
||||||
|
@ -1145,7 +1145,7 @@ namespace SharedLibraryCore
|
|||||||
/// </summary>
|
/// </summary>
|
||||||
/// <returns></returns>
|
/// <returns></returns>
|
||||||
public static bool IsDevelopment =>
|
public static bool IsDevelopment =>
|
||||||
Environment.GetEnvironmentVariable("ASPNETCORE_ENVIRONMENT") == "Development";
|
Environment.GetEnvironmentVariable("ASPNETCORE_ENVIRONMENT") == "Development" || AppContext.TryGetSwitch("IsDevelop", out _);
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// replaces any directory separator chars with the platform specific character
|
/// replaces any directory separator chars with the platform specific character
|
||||||
|
Loading…
Reference in New Issue
Block a user