azure - Serilog Seq Sink 并不总是捕获事件

标签 azure asp.net-core serilog seq-logging getseq

我在 Azure 实例(Windows Server 2012 R2 Datacenter)上运行 Seq,并从本地工作站上运行的控制台应用程序使用 Serilog 进行日志记录。我配置了 3 个接收器 - 文件、控制台和序列。我也在 dnxcore50 上运行(以防万一您认为我的设置不够狡猾)。我的所有事件 100% 都显示在控制台和文件中。 Seq 在 5 次或更多运行中仅捕获大约 1 个事件,也就是说,它将捕获该运行的所有事件,或者不捕获任何事件。我正在使用免费的单用户许可证来评估 Seq,并且没有发现任何表明存在任何限制会导致此行为的信息。

我已经在记录器上设置了 SelfLog.Out,但是除了我添加的测试行以确保自记录至少可以写入指定文件之外,它根本不记录任何内容。

public abstract class SerilogBaseLogger<T> : SerilogTarget
{
    protected SerilogBaseLogger(SerilogOptions options, string loggerType)
    {
        LoggerConfiguration = new LoggerConfiguration();
        Options = options;
        LevelSwitch.MinimumLevel = options.LogLevel.ToSerilogLevel();

        var file = File.CreateText(Path.Combine(options.LogFilePath, string.Format("SelfLog - {0}.txt", loggerType)));
        file.AutoFlush = true;
        SelfLog.Out = file;
        SelfLog.WriteLine("Testing self log.");
    }

    // ... snip ....
}

public class SerilogSeqTarget<T> : SerilogBaseLogger<T>
{
    public string Server => Options.Seq.Server;

    public SerilogSeqTarget(SerilogOptions options)
        : base(options, string.Format("SerilogSeqTarget[{0}]", typeof(T)))
    {
        LoggerConfiguration
            .WriteTo
            .Seq(Server);

        InitializeLogger();
    }

    public override string ToString()
    {
        return string.Format("SerilogSeqTarget<{0}>", typeof(T).Name);
    }
}

public class SerilogLoggerFactory<TType> : LoggerFactory<SerilogTarget>
{
    // .... snip ....

    public override IMyLoggerInterface GetLogger()
    {
        var myLogger = new SerilogDefaultLogger()
            .AddTarget(SerilogTargetFactory<TType>.GetFileTarget(Options))
            .AddTarget(SerilogTargetFactory<TType>.GetConsoleTarget(Options))
            .AddTarget(SerilogTargetFactory<TType>.GetSeqTarget(Options));

        myLogger.Info("Logger initialized with {@options} and targets: {targets}", Options, ((SerilogDefaultLogger)myLogger).Targets);

        return myLogger;
    }
}

public class SerilogTargetFactory<TType>
{
    // ... snip ...

    public static SerilogTarget GetSeqTarget(SerilogOptions options)
    {
        return !string.IsNullOrEmpty(options.Seq.Server)
                   ? new SerilogSeqTarget<TType>(options)
                   : null;
    }
}

有什么建议吗?这是否只是处于前沿、使用预发布的所有内容的副作用(尽管在这种情况下,我预计事情会持续失败)?

最佳答案

当以 dnxcore50/CoreCLR 为目标时,Serilog 无法 Hook AppDomain 关闭以保证任何缓冲的消息始终被刷新。 (该配置文件中不存在AppDomain :-)

有几个选项:

  1. 在关闭时处置记录器(尤其是 Seq 记录器):

    (记录器为 IDisposable)?.Dispose();

  2. 使用静态Log类并在关闭时调用其CloseAndFlush()方法:

    Log.CloseAndFlush();

后者 - 使用静态 Log 类而不是各种单独的 ILogger 实例 - 可能是最快且最容易使用的,但它具有完全相同的功能效果与处理记录器一样,因此任何一种方法都可以做到这一点。

关于azure - Serilog Seq Sink 并不总是捕获事件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34189586/

相关文章:

c# - 将请求 header 添加到 Nancy 应用程序的 Application Insights 遥测

asp.net-core - 使用 Serilog 同时记录到 AzureTableStorage

asp.net - 如何在 .NET Framework for Serilog 中设置配置

azure - 如何使用 Azure Scheduler 配置和进行测试

angular - 升级到.Net Core 3.1后,“事件/就绪”探针失败

python - Python 中的 Azure 表存储查询结果实体中未出现时间戳

visual-studio - .NET vNext 只使用引用包中的一个 dll

c# - Serilog 不将日志写入文件

azure - ARM 模板 - 使用 MSDeploy 以及 Azure 存储 Blob 中的压缩代码来部署函数

asp.net - 不允许用于访问此页面的 HTTP 谓词 Web Api IIS 7