小编Naz*_*nyk的帖子

ASP.Net Core 如何在应用程序即将停止时对数据库执行最终操作

我正在开发 ASP.NET Core 3.1 应用程序。我想在我的应用程序即将停止时执行一些与数据库相关的最终操作。为此,我试图在我的范围服务中调用一个函数,该函数是数据库和 ASP.NET Core 之间的附加层。

启动文件

public void Configure(IApplicationBuilder app, IHostApplicationLifetime lifetime)
{
    lifetime.ApplicationStopping.Register(() =>
    {
        app.ApplicationServices.GetService<MyService>().SynchronizeChanges();
    });
}
Run Code Online (Sandbox Code Playgroud)

但不幸的Cannot resolve scoped service 'MyApplication.Services.MyService' from root provider是,我在尝试时出错。

c# database asp.net-core

7
推荐指数
1
解决办法
90
查看次数

ASP.Net Core Serilog 如何在运行时读取日志文件

我正在开发 ASP.NET Core 3.1 应用程序。我想将事件记录到文件中并能够在应用程序运行时读取它们。为此,我尝试使用Serilog.Extensions.Logging.File NuGet 包,然后指定日志文件路径如下:

启动文件

 public void Configure(IApplicationBuilder app, ILoggerFactory logFactory)
 {
     logFactory.AddFile("Log");
 }
Run Code Online (Sandbox Code Playgroud)

任何以这种方式读取或写入文件的尝试

string ReadLog(string logPath)
{
    return System.IO.File.ReadAllText(logPath);
}
Run Code Online (Sandbox Code Playgroud)

System.IO.IOException: 'The process cannot access the file {log-path} because it is being used by another process.'异常结束。


编辑:我已经安装了Serilog.AspNetCore并进行了如下所示的更改,同时还从配置功能中删除了 logFactory。但异常继续发生。


程序.cs

public static int Main(string[] args)
{
    Log.Logger = new LoggerConfiguration()
        .MinimumLevel.Debug()
        .MinimumLevel.Override("Microsoft", LogEventLevel.Information)
        .Enrich.FromLogContext()
        .WriteTo.Console()
        .WriteTo.File(
            "Logs/log-.txt", 
            shared: true,
            flushToDiskInterval: TimeSpan.FromSeconds(5),
            rollingInterval: RollingInterval.Day)
        .CreateLogger();

    try
    {
        Log.Information("Starting web host"); …
Run Code Online (Sandbox Code Playgroud)

c# logging serilog asp.net-core

6
推荐指数
2
解决办法
3181
查看次数

ASP.NET Core 3.1 | ASP.NET Core 3.1 如何使用服务集合进行单元测试并使用 NullLogger 作为 ILogger 实现

我想进行单元测试来测试我的服务,但它们需要 ILogger 进行依赖项注入,并且我想使用 IServiceProvider 存储和访问它们。对于这种情况,有 NullLogger,但我将其添加到 ServiceCollection 中的所有尝试都以以下异常结束:
Unable to resolve service for type 'Microsoft.Extensions.Logging.ILogger 1[MyProject.MyService]' while attempting to activate 'MyProject.MyService'.

public class MyService
{
    private readonly ILogger<MyService> _logger;

    public MyService(ILogger<MyService> logger)
    {
        _logger = logger;
    }
}

public class Tests
{
    private ServiceCollection _services;
    private IServiceProvider _serviceProvider;

    [SetUp]
    public void Setup()
    {
        _services = new ServiceCollection();
        /* ---------------------------\/--------------------------- */

        _services.AddSingleton<ILogger, NullLogger>(); // Doesn't works.
        // _services.AddSingleton<ILoggerFactory, NullLoggerFactory>() Another attempt.

        /* ---------------------------/\--------------------------- */
        _services.AddSingleton<MyService>();
        _serviceProvider = _services.BuildServiceProvider();
    }

    [Test]
    public …
Run Code Online (Sandbox Code Playgroud)

c# logging unit-testing asp.net-core

5
推荐指数
1
解决办法
1351
查看次数

标签 统计

asp.net-core ×3

c# ×3

logging ×2

database ×1

serilog ×1

unit-testing ×1