我创建了一个 .Net 类库 (4.6.2) 并创建了由其他接口(如控制台应用程序)调用的 serilog 实现。现在,当我使用文件接收器类型时,日志被写入文件,但使用 MSSQL 接收器,它没有这样做。使用 autoCreateTable 选项提供的列选项创建日志表
ILogger logger = new LoggerConfiguration()
.WriteTo.MSSqlServer(connectionString,
tableName,
autoCreateSqlTable: autoCreateSqlTable,
restrictedToMinimumLevel: LogEventLevel.Verbose,
columnOptions: GetSQLSinkColumnOptions(),
batchPostingLimit: batchPostingLimit)
.CreateLogger();
Run Code Online (Sandbox Code Playgroud)
我还启用了 serilog 的自我记录,但没有显示任何异常。没有找到任何有用的解决方案。
然而,日志表正在生成。我已经检查了用户的权限,并且也有正确的权限。
下面是代码的快照。
public static class GenericLogger
{
private static ILogger _usageLogger;
private static ILogger _errorLogger;
static GenericLogger()
{
var logTypes = LogConfigurationHelper.GetLogTypes();
if (logTypes != null && logTypes.Count > 0)
{
foreach (var logType in logTypes)
{
ConfigureLogger(logType.Id); // Intitalizes logs based on
//configuration.
}
}
Serilog.Debugging.SelfLog.Enable(msg =>
{
Debug.Print(msg);
Debugger.Break();
});
}
///The write log function
///
public static void WriteError(LogDetail infoToLog)
{
if (infoToLog.Exception != null)
{
infoToLog.Message = GetMessageFromException(infoToLog.Exception);
}
_errorLogger.Write(LogEventLevel.Information,
"{Timestamp}{Product}{Layer}{Location}{Message}" +
"{Hostname}{UserId}{UserName}{Exception}{ElapsedMilliseconds}" +
"{CorrelationId}{CustomException}{AdditionalInfo}",
infoToLog.TimeStamp, infoToLog.Product, infoToLog.Layer, infoToLog.Location, infoToLog.Message,
infoToLog.Hostname, infoToLog.UserId, infoToLog.UserName, infoToLog.Exception?.ToCustomString(),
infoToLog.ElapsedMilliseconds, infoToLog.CorrelationId, infoToLog.CustomException,
infoToLog.AdditionalInfo);
// To add ((IDisposable) _errrorLog).Dispose();
}
}
Run Code Online (Sandbox Code Playgroud)
以下是一些可以帮助您进行故障排除的想法:
您是否仅使用Verbose或Debug事件进行测试?这可能是原因。您没有为 Serilog 指定全局最低级别(您只为接收器指定了最低级别,它充当过滤器),默认最小值为Information,这意味着Verbose并且Debug正在被忽略...MinimumLevel为 Serilog指定全局:
ILogger logger = new LoggerConfiguration()
.MinimumLevel.Verbose()
.WriteTo.MSSqlServer(connectionString,
tableName,
autoCreateSqlTable: autoCreateSqlTable,
restrictedToMinimumLevel: LogEventLevel.Verbose,
columnOptions: GetSQLSinkColumnOptions(),
batchPostingLimit: batchPostingLimit)
.CreateLogger();
Run Code Online (Sandbox Code Playgroud)
你在处理你的记录器吗?Serilog.Sinks.MSSqlServer是“定期批处理接收器”,因此您需要确保在最后处置记录器以强制它将日志刷新到数据库。请参阅记录器的生命周期。
((IDisposable) logger).Dispose();
Run Code Online (Sandbox Code Playgroud)
即使您正在使用1for batchPostingLimit,它5在将日志发送到数据库之前默认等待几秒钟。如果您的应用程序在该时间段之前关闭并且您没有处理记录器,则消息将丢失。
为了故障排除,使用AuditTo而不是WriteTo(并删除batchPostingLimit不适用于审计的)。WriteTo是安全的,会吃掉任何异常,同时AuditTo会让异常冒泡。
ILogger logger = new LoggerConfiguration()
.AuditTo.MSSqlServer(
connectionString,
tableName,
restrictedToMinimumLevel: LogEventLevel.Verbose,
autoCreateSqlTable: true)
.CreateLogger();
Run Code Online (Sandbox Code Playgroud)
当然,一旦你弄清楚出了什么问题,就回到WriteTo.
| 归档时间: |
|
| 查看次数: |
4574 次 |
| 最近记录: |