完善日志内容与日志记录
This commit is contained in:
parent
de35b35d7b
commit
f7e71f598d
@ -5,6 +5,7 @@ using System;
|
|||||||
using System.Diagnostics;
|
using System.Diagnostics;
|
||||||
using System.IO;
|
using System.IO;
|
||||||
using System.Net;
|
using System.Net;
|
||||||
|
using System.Text;
|
||||||
using System.Threading.Tasks;
|
using System.Threading.Tasks;
|
||||||
|
|
||||||
namespace FastGithub.ReverseProxy
|
namespace FastGithub.ReverseProxy
|
||||||
@ -55,19 +56,28 @@ namespace FastGithub.ReverseProxy
|
|||||||
}
|
}
|
||||||
|
|
||||||
var exception = context.GetForwarderErrorFeature()?.Exception;
|
var exception = context.GetForwarderErrorFeature()?.Exception;
|
||||||
if (IsError(exception) == false)
|
if (exception == null)
|
||||||
{
|
{
|
||||||
this.logger.LogInformation(message);
|
this.logger.LogInformation(message);
|
||||||
}
|
}
|
||||||
else
|
else if (IsError(exception))
|
||||||
{
|
{
|
||||||
this.logger.LogError($"{message}{Environment.NewLine}{exception}");
|
this.logger.LogError($"{message}{Environment.NewLine}{exception}");
|
||||||
}
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
this.logger.LogWarning($"{message}{Environment.NewLine}{GetMessage(exception)}");
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private static bool IsError(Exception? exception)
|
/// <summary>
|
||||||
|
/// 是否为错误
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="exception"></param>
|
||||||
|
/// <returns></returns>
|
||||||
|
private static bool IsError(Exception exception)
|
||||||
{
|
{
|
||||||
if (exception == null || exception is OperationCanceledException)
|
if (exception is OperationCanceledException)
|
||||||
{
|
{
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
@ -79,5 +89,24 @@ namespace FastGithub.ReverseProxy
|
|||||||
|
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 获取异常信息
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="exception"></param>
|
||||||
|
/// <returns></returns>
|
||||||
|
private static string GetMessage(Exception exception)
|
||||||
|
{
|
||||||
|
var ex = exception;
|
||||||
|
var builder = new StringBuilder();
|
||||||
|
|
||||||
|
while (ex != null)
|
||||||
|
{
|
||||||
|
var type = ex.GetType();
|
||||||
|
builder.Append(type.Namespace).Append(".").Append(type.Name).Append(": ").AppendLine(ex.Message);
|
||||||
|
ex = ex.InnerException;
|
||||||
|
}
|
||||||
|
return builder.ToString();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@ -22,7 +22,8 @@
|
|||||||
|
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
<PackageReference Include="Serilog.AspNetCore" Version="4.1.0" />
|
<PackageReference Include="Serilog.AspNetCore" Version="4.1.0" />
|
||||||
<PackageReference Include="Serilog.Sinks.Console" Version="3.1.1" />
|
<PackageReference Include="Serilog.Sinks.Console" Version="4.0.0" />
|
||||||
|
<PackageReference Include="Serilog.Sinks.File" Version="5.0.0" />
|
||||||
<PackageReference Include="Serilog.Settings.Configuration" Version="3.2.0" />
|
<PackageReference Include="Serilog.Settings.Configuration" Version="3.2.0" />
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
|
|
||||||
|
|||||||
@ -58,10 +58,12 @@ namespace FastGithub
|
|||||||
});
|
});
|
||||||
webBuilder.UseSerilog((hosting, logger) =>
|
webBuilder.UseSerilog((hosting, logger) =>
|
||||||
{
|
{
|
||||||
|
var template = "{Timestamp:O} [{Level:u3}]{NewLine}{SourceContext}{NewLine}{Message:lj}{NewLine}{Exception}{NewLine}";
|
||||||
logger
|
logger
|
||||||
.ReadFrom.Configuration(hosting.Configuration)
|
.ReadFrom.Configuration(hosting.Configuration)
|
||||||
.Enrich.FromLogContext()
|
.Enrich.FromLogContext()
|
||||||
.WriteTo.Console(outputTemplate: "{Timestamp:O} [{Level:u3}]{NewLine}{SourceContext}{NewLine}{Message:lj}{NewLine}{Exception}{NewLine}");
|
.WriteTo.Console(outputTemplate: template)
|
||||||
|
.WriteTo.File(Path.Combine("logs", @"log.txt"), rollingInterval: RollingInterval.Day, outputTemplate: template);
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user