小编Fer*_*mez的帖子

如何配置ILogger将日志输出到特定文件?

我有一个 ASP.NET Core 2 Web 应用程序正在部署到生产环境,并且需要启用日志来排除错误。我在文档中找不到有关 appsettings.json 记录器部分中的架构的任何位置。这是我那里有的:

"Logging": {
    "IncludeScopes": false,
    "LogLevel": {
      "Default": "Debug",
      "System": "Information",
      "Microsoft": "Information" }
    ,"Console": { "IncludeScopes": "true" }
  }
Run Code Online (Sandbox Code Playgroud)

当我从控制台运行 exe 时,日志信息显示在控制台中,但实体框架也在记录日志,因此我看不到自己的消息,因为显示的输出量有限。因此,我需要将日志输出到一个文件,例如 C:\temp\my.log。如何配置 ASP.NET Core 2 来执行此操作?

提前致谢。

logging asp.net-core asp.net-core-2.0

12
推荐指数
2
解决办法
2万
查看次数

在 Startup.ConfigureServices 方法期间访问依赖注入对象

我有一个 ASP.NET Core 3 应用程序,并且使用 AzureAD 进行身份验证。我的 Startup.ConfigureSerivces 方法中有以下几行,其目的是在附加 cookie 时执行一些业务规则。

services.Configure<CookiePolicyOptions>(options => {
    options.CheckConsentNeeded = ctx => true;
    options.MinimumSameSitePolicy = SameSiteMode.None;
    options.OnAppendCookie = ctx => {
        var svc = ctx.Context.RequestServices.GetRequiredService<IUserInformationService>();
        // do something with svc        
    };
});
services.AddAuthentication(AzureADDefaults.AuthenticationScheme)
    .AddAzureAD(options => {
        Configuration.Bind("AzureAd", options);
    });
Run Code Online (Sandbox Code Playgroud)

这效果很好,我IUserInformationService按照方法中的预期获得了注入的对象OnAppendCookie,并且 AzureAD 信息取自 appsettings.json。

然而最近,有关 AzureAD 租户的信息不得驻留在 appsettings.json 中,而是我现在必须查阅数据库。我有一项服务已经查询数据库并获取 AD 设置。就像是:

public interface IAzureADConfiguration {
    void Configure(AzureADOptions options);
}
Run Code Online (Sandbox Code Playgroud)

但是,我找不到在调用时检索注入的服务的方法AddAzureAD。我想要的是这样的:

services.AddAuthentication(AzureADDefaults.AuthenticationScheme)
    .AddAzureAD(options => {
        var svc = ???
        svc.Configure(options); …
Run Code Online (Sandbox Code Playgroud)

c# dependency-injection asp.net-core

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