Skip to content

Commit

Permalink
fix(log): use TryParse
Browse files Browse the repository at this point in the history
  • Loading branch information
GZTimeWalker committed Aug 7, 2023
1 parent 7e8eb44 commit 604dd4d
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 40 deletions.
7 changes: 4 additions & 3 deletions src/GZCTF/Models/Internal/Configs.cs
Original file line number Diff line number Diff line change
Expand Up @@ -157,10 +157,11 @@ public void ToForwardedHeadersOptions(ForwardedHeadersOptions options)
{
// split the network into address and prefix length
var parts = network.Split('/');
if (parts.Length == 2 && int.TryParse(parts[1], out var prefixLength))
if (parts.Length == 2 &&
IPAddress.TryParse(parts[0], out var prefix) &&
int.TryParse(parts[1], out var prefixLength))
{
var address = IPAddress.Parse(parts[0]);
options.KnownNetworks.Add(new IPNetwork(address, prefixLength));
options.KnownNetworks.Add(new IPNetwork(prefix, prefixLength));
}
});

Expand Down
73 changes: 36 additions & 37 deletions src/GZCTF/Utils/LogHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ public static void UseRequestLogging(this WebApplication app)
{
app.UseSerilogRequestLogging(options =>
{
options.MessageTemplate = "[{StatusCode}] @{Elapsed,8:####0.00}ms HTTP {RequestMethod,-6} {RequestPath} From {RemoteIP}";
options.MessageTemplate = "[{StatusCode}] {Elapsed,8:####0.00}ms HTTP {RequestMethod,-6} {RequestPath} @ {RemoteIP}";
options.GetLevel = (context, time, ex) =>
time > 10000 && context.Response.StatusCode != 101 ? LogEventLevel.Warning :
(context.Response.StatusCode > 499 || ex is not null) ? LogEventLevel.Error : LogEventLevel.Debug;
Expand All @@ -110,54 +110,53 @@ public static void UseRequestLogging(this WebApplication app)
private const string LogTemplate = "[{@t:yy-MM-dd HH:mm:ss.fff} {@l:u3}] {Substring(SourceContext, LastIndexOf(SourceContext, '.') + 1)}: {@m} {#if Length(Status) > 0}#{Status} <{UserName}>{#if Length(IP) > 0}@{IP}{#end}{#end}\n{@x}";
private const string InitLogTemplate = "[{@t:yy-MM-dd HH:mm:ss.fff} {@l:u3}] {@m}\n{@x}";

public static Logger GetInitLogger()
public static Serilog.ILogger GetInitLogger()
=> new LoggerConfiguration()
.Enrich.FromLogContext()
.MinimumLevel.Debug()
.MinimumLevel.Override("Microsoft", LogEventLevel.Warning)
.MinimumLevel.Override("AspNetCoreRateLimit", LogEventLevel.Warning)
.MinimumLevel.Override("Microsoft.Hosting.Lifetime", LogEventLevel.Information)
.MinimumLevel.Override("Microsoft.Hosting.Lifetime", LogEventLevel.Warning)
.WriteTo.Async(t => t.Console(
formatter: new ExpressionTemplate(InitLogTemplate, theme: TemplateTheme.Literate),
restrictedToMinimumLevel: LogEventLevel.Debug
))
.CreateLogger();
.CreateBootstrapLogger();

public static Logger GetLogger(IConfiguration configuration, IServiceProvider serviceProvider)
public static Serilog.ILogger GetLogger(IConfiguration configuration, IServiceProvider serviceProvider)
=> new LoggerConfiguration()
.Enrich.FromLogContext()
.MinimumLevel.Debug()
.Filter.ByExcluding(logEvent =>
logEvent.Exception != null &&
logEvent.Exception.GetType() == typeof(OperationCanceledException))
.MinimumLevel.Override("Microsoft", LogEventLevel.Warning)
.MinimumLevel.Override("AspNetCoreRateLimit", LogEventLevel.Warning)
.MinimumLevel.Override("Microsoft.Hosting.Lifetime", LogEventLevel.Warning)
.WriteTo.Async(t => t.Console(
formatter: new ExpressionTemplate(LogTemplate, theme: TemplateTheme.Literate),
restrictedToMinimumLevel: LogEventLevel.Debug
))
.WriteTo.Async(t => t.File(
path: "log/log_.log",
formatter: new ExpressionTemplate(LogTemplate),
rollingInterval: RollingInterval.Day,
fileSizeLimitBytes: 10 * 1024 * 1024,
restrictedToMinimumLevel: LogEventLevel.Debug,
rollOnFileSizeLimit: true,
retainedFileCountLimit: 5,
hooks: new ArchiveHooks(CompressionLevel.Optimal, "log/archive/{UtcDate:yyyy-MM}")
))
.WriteTo.Async(t => t.PostgreSQL(
connectionString: configuration.GetConnectionString("Database"),
tableName: "Logs",
respectCase: true,
columnOptions: ColumnWriters,
restrictedToMinimumLevel: LogEventLevel.Information,
period: TimeSpan.FromSeconds(30)
))
.WriteTo.SignalR(serviceProvider)
.CreateLogger();
.Enrich.FromLogContext()
.MinimumLevel.Debug()
.Filter.ByExcluding(logEvent =>
logEvent.Exception != null &&
logEvent.Exception.GetType() == typeof(OperationCanceledException))
.MinimumLevel.Override("Microsoft", LogEventLevel.Warning)
.MinimumLevel.Override("AspNetCoreRateLimit", LogEventLevel.Warning)
.MinimumLevel.Override("Microsoft.Hosting.Lifetime", LogEventLevel.Warning)
.WriteTo.Async(t => t.Console(
formatter: new ExpressionTemplate(LogTemplate, theme: TemplateTheme.Literate),
restrictedToMinimumLevel: LogEventLevel.Debug
))
.WriteTo.Async(t => t.File(
path: "log/log_.log",
formatter: new ExpressionTemplate(LogTemplate),
rollingInterval: RollingInterval.Day,
fileSizeLimitBytes: 10 * 1024 * 1024,
restrictedToMinimumLevel: LogEventLevel.Debug,
rollOnFileSizeLimit: true,
retainedFileCountLimit: 5,
hooks: new ArchiveHooks(CompressionLevel.Optimal, "log/archive/{UtcDate:yyyy-MM}")
))
.WriteTo.Async(t => t.PostgreSQL(
connectionString: configuration.GetConnectionString("Database"),
tableName: "Logs",
respectCase: true,
columnOptions: ColumnWriters,
restrictedToMinimumLevel: LogEventLevel.Information,
period: TimeSpan.FromSeconds(30)
))
.WriteTo.SignalR(serviceProvider)
.CreateLogger();
}

public class TimeColumnWriter : ColumnWriterBase
Expand Down

0 comments on commit 604dd4d

Please sign in to comment.