Serilog-将级别转换为Syslog标准

发布于 2025-02-07 16:45:15 字数 671 浏览 4 评论 0原文

我正在使用Azure IoT Edge容器,我们正在使用Serilog。 Microsoft建议以本文所示的格式输出日志文本: https://learn.microsoft.com/en-us/azure/azure/iot-ege/iot-egge/how-to-to-to-to-to-to-regrieve-iot-iot-egge--ge----------------------- logs?view = iotedge-2020-11 。它会将这样的线输出到控制台:

< 6> 2022-06-15 13:05:44.790 +00:00 [inf] - 接收到直接方法调用 - ping

我的问题是 - 如何显示两次级别?第二个很容易({级别:u3}),但是如何在行开头将级别转换为syslog编号?数字参考是在上面文章的开头。我考虑过文本格式器,但是我需要格式的两种不同的方式,并且不确定这是否有效。

想法?

I am working on an Azure IoT Edge container and we are using Serilog. Microsoft recommends outputting the log text to the console window in the format shown in this article: https://learn.microsoft.com/en-us/azure/iot-edge/how-to-retrieve-iot-edge-logs?view=iotedge-2020-11. It would output a line like this to the console:

<6> 2022-06-15 13:05:44.790 +00:00 [INF] - Received direct method call - ping

My question is - how can I display the level twice? The second is easy ({level:u3}), but how can I convert the level to a syslog number at the beginning of the line? The number reference is at the beginning of the article above. I thought about a text formatter, but I need the level formatted two different ways and was not sure that would work.

Thoughts?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

十级心震 2025-02-14 16:45:15

我把它与文本形式的人一起工作

internal class ConsoleTextFormatter : ITextFormatter
    {
        public void Format(LogEvent logEvent, TextWriter output)
        {
            var sb = new StringBuilder();
            sb.AppendLine($"<{GetSysLogLevel(logEvent.Level)}> {logEvent.Timestamp:O} [{GetThreeLetterLevel(logEvent.Level)}] - {logEvent.MessageTemplate}");
            if (logEvent.Exception != null)
                sb.AppendLine(logEvent.Exception.ToString());
            foreach (var property in logEvent.Properties.Where(p => p.Key != "SourceContext"))
            {
                sb.AppendLine($"{property.Key}={property.Value}");
            }

            output.Write(sb.ToString());
        }

        // these are in a static class that I cannot access (https://github.com/serilog/serilog/blob/dev/src/Serilog/Formatting/Display/LevelOutputFormat.cs),
        // so I have hard coded them here
        private string GetThreeLetterLevel(LogEventLevel level)
        {
            return level switch
            {
                LogEventLevel.Debug => "DBG",
                LogEventLevel.Error => "ERR",
                LogEventLevel.Fatal => "FTL",
                LogEventLevel.Information => "INF",
                LogEventLevel.Verbose => "VRB",
                LogEventLevel.Warning => "WRN",
                _ => throw new NotSupportedException("Unknown log level")
            };
        }

        private string GetSysLogLevel(LogEventLevel level)
        {
            return level switch
            {
                LogEventLevel.Debug => "7",
                LogEventLevel.Error => "3",
                LogEventLevel.Fatal => "2", // critical
                LogEventLevel.Information => "6",
                LogEventLevel.Verbose => "7",   // debug also
                LogEventLevel.Warning => "4",
                _ => throw new NotSupportedException("Unknown log level")
            };
        }

I got it to work with a TextFormatter

internal class ConsoleTextFormatter : ITextFormatter
    {
        public void Format(LogEvent logEvent, TextWriter output)
        {
            var sb = new StringBuilder();
            sb.AppendLine(
quot;<{GetSysLogLevel(logEvent.Level)}> {logEvent.Timestamp:O} [{GetThreeLetterLevel(logEvent.Level)}] - {logEvent.MessageTemplate}");
            if (logEvent.Exception != null)
                sb.AppendLine(logEvent.Exception.ToString());
            foreach (var property in logEvent.Properties.Where(p => p.Key != "SourceContext"))
            {
                sb.AppendLine(
quot;{property.Key}={property.Value}");
            }

            output.Write(sb.ToString());
        }

        // these are in a static class that I cannot access (https://github.com/serilog/serilog/blob/dev/src/Serilog/Formatting/Display/LevelOutputFormat.cs),
        // so I have hard coded them here
        private string GetThreeLetterLevel(LogEventLevel level)
        {
            return level switch
            {
                LogEventLevel.Debug => "DBG",
                LogEventLevel.Error => "ERR",
                LogEventLevel.Fatal => "FTL",
                LogEventLevel.Information => "INF",
                LogEventLevel.Verbose => "VRB",
                LogEventLevel.Warning => "WRN",
                _ => throw new NotSupportedException("Unknown log level")
            };
        }

        private string GetSysLogLevel(LogEventLevel level)
        {
            return level switch
            {
                LogEventLevel.Debug => "7",
                LogEventLevel.Error => "3",
                LogEventLevel.Fatal => "2", // critical
                LogEventLevel.Information => "6",
                LogEventLevel.Verbose => "7",   // debug also
                LogEventLevel.Warning => "4",
                _ => throw new NotSupportedException("Unknown log level")
            };
        }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文