Serilog。当使用简单的for循环进行日志记录时,丢失了日志。

问题描述 投票:1回答:1

我已经开始使用Serilog,并进行了测试。Dot Net Core 3.1 Console应用程序.

在我的简单项目中,我正在循环和记录。Serilog记录在 控制台文件.

但是我发现,在10,000条日志之后,日志被遗漏了。在10,000条之前,所有的日志都是在控制台和文件中的序列,但是在10,000条之后,日志开始遗漏在以下两个地方 控制台文件. 例如,10000之后的29483。

另外,调用Log.CloseAndFlush()函数来刷新所有的日志并没有工作,因为我没有看到日志条目在最后的 控制台文件.

2020-05-26 12:12:38.575 | [INFO] | Thread-1 | hello:1
........ Logs are in sequencial order ........
2020-05-26 12:12:38.575 | [INFO] | Thread-1 | hello:9993
2020-05-26 12:12:38.575 | [INFO] | Thread-1 | hello:9994
2020-05-26 12:12:38.575 | [INFO] | Thread-1 | hello:9995
2020-05-26 12:12:38.575 | [INFO] | Thread-1 | hello:9996
2020-05-26 12:12:38.575 | [INFO] | Thread-1 | hello:9997
2020-05-26 12:12:38.575 | [INFO] | Thread-1 | hello:9998
2020-05-26 12:12:38.575 | [INFO] | Thread-1 | hello:9999
2020-05-26 12:12:38.575 | [INFO] | Thread-1 | hello:10000
2020-05-26 12:12:38.637 | [INFO] | Thread-1 | hello:29483
2020-05-26 12:12:38.689 | [INFO] | Thread-1 | hello:62627
2020-05-26 12:12:38.693 | [INFO] | Thread-1 | hello:64740
2020-05-26 12:12:38.694 | [INFO] | Thread-1 | hello:65624
2020-05-26 12:12:38.695 | [INFO] | Thread-1 | hello:66169
2020-05-26 12:12:38.697 | [INFO] | Thread-1 | hello:66662
2020-05-26 12:12:38.697 | [INFO] | Thread-1 | hello:67040
2020-05-26 12:12:38.698 | [INFO] | Thread-1 | hello:67432
2020-05-26 12:12:38.698 | [INFO] | Thread-1 | hello:67828
2020-05-26 12:12:38.699 | [INFO] | Thread-1 | hello:68083
2020-05-26 12:12:38.700 | [INFO] | Thread-1 | hello:68386
2020-05-26 12:12:38.700 | [INFO] | Thread-1 | hello:68756
2020-05-26 12:12:38.701 | [INFO] | Thread-1 | hello:69387
2020-05-26 12:12:38.702 | [INFO] | Thread-1 | hello:69904
2020-05-26 12:12:38.703 | [INFO] | Thread-1 | hello:70296
2020-05-26 12:12:38.703 | [INFO] | Thread-1 | hello:70681
2020-05-26 12:12:38.704 | [INFO] | Thread-1 | hello:71093
2020-05-26 12:12:38.706 | [INFO] | Thread-1 | hello:72488
2020-05-26 12:12:38.706 | [INFO] | Thread-1 | hello:72945
2020-05-26 12:12:38.707 | [INFO] | Thread-1 | hello:73590
2020-05-26 12:12:38.708 | [INFO] | Thread-1 | hello:73975
2020-05-26 12:12:38.708 | [INFO] | Thread-1 | hello:74383
2020-05-26 12:12:38.709 | [INFO] | Thread-1 | hello:74783
2020-05-26 12:12:38.709 | [INFO] | Thread-1 | hello:75183
2020-05-26 12:12:38.711 | [INFO] | Thread-1 | hello:75644
2020-05-26 12:12:38.712 | [INFO] | Thread-1 | hello:76087
2020-05-26 12:12:38.713 | [INFO] | Thread-1 | hello:76719

我看了其他开发者面临的日志丢失的问题,但都与我的问题无关。

程序.cs

using Microsoft.Extensions.Configuration;
using Serilog;
using Serilog.Core;
using System;

namespace MyApp
{
    public class Program
    {
        static void Main(string[] args)
        {
            var l_config = new ConfigurationBuilder().AddJsonFile("appsettings.json").Build();

            Logger l_log = new LoggerConfiguration().ReadFrom.Configuration(l_config).CreateLogger();
            Log.Logger = l_log;

            for (long l_index = 0; l_index < 100000L; l_index++)
            {
                Log.Information("hello:" + l_index);
            }
            Log.Information("FINISHED");
            Log.CloseAndFlush();
            Console.ReadKey();
        }

        ~Program()
        {
            Log.CloseAndFlush();
        }
    }
}

appsettings.json

{
    "Serilog": {
        "Using": ["Serilog.Sinks.Console"],
        "MinimumLevel": "Debug",
        "WriteTo": [{
                "Name": "Async",
                "Args": {
                    "configure": [{
                            "Name": "Console",
                            "Args": {
                                "MinimumLevel": "Verbose",
                                "outputTemplate": "{Timestamp:yyyy-MM-dd HH:mm:ss.fff} | [{Level:u4}] | Thread-{ThreadId} | {Message:lj}{NewLine}{Exception}"
                            }                           
                        }, {
                            "Name": "File",
                            "Args": {
                                "MinimumLevel": "Verbose",
                                "path": "Logs\\log..txt",                               
                                "outputTemplate": "{Timestamp:yyyy-MM-dd HH:mm:ss.fff} | [{Level:u4}] | Thread-{ThreadId} | {Message:lj}{NewLine}{Exception}",
                                "rollingInterval": "Hour",
                            }
                        }
                    ]
                }
            }
        ],
        "Enrich": ["FromLogContext", "WithThreadId"],
        "Destructure": [{
                "Name": "ToMaximumDepth",
                "Args": {
                    "maximumDestructuringDepth": 4
                }
            }, {
                "Name": "ToMaximumStringLength",
                "Args": {
                    "maximumStringLength": 100
                }
            }, {
                "Name": "ToMaximumCollectionCount",
                "Args": {
                    "maximumCollectionCount": 10
                }
            }
        ],
        "Properties": {
            "Application": "MyApp"
        }
    }
}

使用Nuget包。

  • Serilog.dll
  • Serilog.Enrichers.Environment.dll。
  • Serilog.Enrichers.Thread.dll。
  • Serilog.Settings.Configuration.dll。
  • Serilog.Sinks.Async.dll。
  • Serilog.Sinks.Console.dll。

我很抱歉在这里放了很多信息,但这是需要的,所以我们可以指出问题。

logging .net-core serilog
1个回答
1
投票

这是因为缓冲区大小喂 Async() 默认情况下是固定在10,000个项目,满了也不会被屏蔽。

要改变这一情况,请添加 blockWhenFull: true:

    "WriteTo": [{
            "Name": "Async",
            "Args": {
                "blockWhenFull": true,
                "configure": [{

更多信息 此处此处.

© www.soinside.com 2019 - 2024. All rights reserved.