c# - EF Core 日志记录的内存泄漏

标签 c# linux .net-core out-of-memory

我有一个运行最新的 Ubuntu LTS 的小型 Digital Ocean Droplet(1GB 内存)。我创建了一个小的 DotNet 核心 3.1 MVC Web 应用程序(+ DotNet 核心标识),它最终达到任务限制,然后抛出(错误的)OOM 异常。
来自 journalctl 的代表性错误消息:

 Microsoft.AspNetCore.Server.Kestrel[13] Connection id "0HM1CRI33JJJE", Request id "0HM1CRI33JJJE:0000043E": An unhandled exception was thrown by the application. System.OutOfMemoryException: Exception of type 'System.OutOfMemoryException' was thrown.
 at System.Threading.Thread.StartInternal()
 at Microsoft.Extensions.Logging.Console.ConsoleLoggerProvider..ctor(IOptionsMonitor`1 options)
 at System.RuntimeMethodHandle.InvokeMethod(Object target, Object[] arguments, Signature sig, Boolean constructor, Boolean wrapExceptions)
 at System.Reflection.RuntimeConstructorInfo.Invoke(BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture)
 at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteRuntimeResolver.VisitCache(ServiceCallSite callSite, RuntimeResolverContext context, ServiceProviderEngineScope serviceProviderEngine, RuntimeResolverLock lockType)
 at ... (etc)
重新启动时的服务状态:
Site.service - Description here
     Loaded: loaded (/etc/systemd/system/Site.service; enabled; vendor preset: enabled)
     Active: active (running) since Wed 2020-07-22 17:08:51 UTC; 48s ago
   Main PID: 656469 (Site)
      Tasks: 21 (limit: 1075)
     Memory: 103.4M
     CGroup: /system.slice/Site.service
问题激活的状态输出:
 Site.service - Description here
     Loaded: loaded (/etc/systemd/system/Site.service; enabled; vendor preset: enabled)
     Active: active (running) since Wed 2020-07-22 17:08:51 UTC; 11min ago
   Main PID: 656469 (Site)
      Tasks: 1075 (limit: 1075)
     Memory: 176.1M
     CGroup: /system.slice/Site.service

最佳答案

来自 https://docs.microsoft.com/en-us/ef/core/logging-events-diagnostics/extensions-logging?tabs=v3

public static readonly ILoggerFactory MyLoggerFactory
= LoggerFactory.Create(builder => { builder.AddConsole(); });

protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
=> optionsBuilder
    .UseLoggerFactory(MyLoggerFactory)
    .UseSqlServer(@"Server=(localdb)\mssqllocaldb;Database=EFLogging;ConnectRetryCount=0");

关于c# - EF Core 日志记录的内存泄漏,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63038803/

相关文章:

linux - 使用 gcc 编译胖可执行文件

unit-testing - dotnet test 仅测试解决方案上的项目

c# - 如何模拟 Entity Framework 的接口(interface)?

c# - WP 8.1应用程序未连接调试器时崩溃

java - 我在哪里可以设置 notes.ini 位置?

linux - Linux 中的远程 Shell

ajax - ASP.NET Core FromBody 在 AJAX POST 上为空,但在 Postman 中填充

Azure SQL 无服务器在连接尝试时不会唤醒

c# - Entity Framework 包含没有关系

c# - 如何调用线程中使用的代码