web-dev-qa-db-fra.com

Où l'API de journalisation ASP.NET Core en tant que journaux de magasin par défaut?

Dans ASP.NET Core 2.0, j'utilise API de journalisation par défaut . Mon application est hébergée en tant qu'application Web Azure.

Ma question: où sont-ils sortis? Et comment puis-je le modifier?

(Je n'en ai pas besoin dans ma base de données ou mon système de fichiers pour l'instant, il suffit de lire les journaux récents pour un débogage).

CE QUE JE FAIS:

Dans mon Startup.cs fichier j'ai injecté la journalisation:

private readonly ILogger<Startup> _logger;

public Startup(IConfiguration configuration, ILogger<Startup> logger)
{
    _logger = logger;
    Configuration = configuration;
}

Et j'écris ensuite quelques:

_logger.LogInformation("Configure RUNNING");

Cependant, je n'ai pu trouver aucun de ces éléments dans un journal à l'intérieur du portail FTP/Azure.

6
Lars Holdgaard

Après avoir joué pendant une heure, j'ai compris comment cela se joue ensemble dans l'application asn ASP.NET Core.

Tout d'abord, je recommanderais de regarder cette vidéo YouTube: https://www.youtube.com/watch?v=icwD6xkyrsc .

Comment le résoudre

ÉTAPE 1:

Accédez à votre Startup.cs classe. Dans la méthode Configure, assurez-vous qu'elle a la signature suivante:

public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory)

Le mien n'avait pas le ILoggerFactory. Cependant, vous devez ajouter celui-ci. Il est par défaut injecté dans la classe par ASP.NET Core.

ÉTAPE 2:

Configurez votre fournisseur.

J'ai configuré les deux suivants:

loggerFactory.AddDebug();
loggerFactory.AddAzureWebAppDiagnostics();

AddAzureWebAppDiagnostics vient de le package Ojisah mentionné dans sa réponse.

ÉTAPE 3:

Nous pouvons maintenant commencer la journalisation.

Exemple dans mon HomeController:

    private readonly ILogger<HomeController> _logger;

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

    public IActionResult Index()
    {
        _logger.LogInformation("TEST INDEX LOGGER");
        return View();
    }

ÉTAPE 4: Assurez-vous que votre niveau de journal correspond à vos attentes

Regarder votre appsettings.json pour vous assurer que le niveau d'avertissement correspond à vos attentes:

{
  "Logging": {
    "IncludeScopes": false,
    "LogLevel": {
      "Default": "Information"
    }
  }
}

ÉTAPE 5: Voir les journaux

Dans Azure, j'ai configuré le Diagnostics logs tab, je l'ai configuré pour qu'il se connecte à mes blobs:

enter image description here

ÉTAPE 6:

Vous pouvez maintenant télécharger et voir les fichiers journaux qui se trouvent dans votre BLOB.

Exemple de mon fichier journal où nous pouvons voir le journal de mon HomeController:

2018-03-05 14:15:32.489 +00:00 [Information] Microsoft.AspNetCore.Mvc.Internal.ControllerActionInvoker: Executed action Likvido.Website.Main.Controllers.HomeController.Index (Likvido.Website.Main) in 1.6257ms
2018-03-05 14:15:32.489 +00:00 [Information] Microsoft.AspNetCore.Hosting.Internal.WebHost: Request finished in 2.0892ms 200 text/html; charset=utf-8
2018-03-05 14:15:32.608 +00:00 [Information] Microsoft.AspNetCore.Hosting.Internal.WebHost: Request starting HTTP/1.1 GET http://www.likvido.dk/js/site.min.js  
2018-03-05 14:15:32.610 +00:00 [Information] Microsoft.AspNetCore.Hosting.Internal.WebHost: Request finished in 2.0154ms 302 
2018-03-05 14:15:32.844 +00:00 [Information] Microsoft.AspNetCore.Hosting.Internal.WebHost: Request starting HTTP/1.1 GET http://likvido.dk/js/site.min.js  
2018-03-05 14:15:32.845 +00:00 [Information] Microsoft.AspNetCore.Hosting.Internal.WebHost: Request finished in 0.571ms 404 
2018-03-05 14:15:46.878 +00:00 [Information] Microsoft.AspNetCore.Hosting.Internal.WebHost: Request starting HTTP/1.1 GET http://www.likvido.dk/  
2018-03-05 14:15:46.878 +00:00 [Information] Microsoft.AspNetCore.Mvc.Internal.ControllerActionInvoker: Executing action method Likvido.Website.Main.Controllers.HomeController.Index (Likvido.Website.Main) with arguments ((null)) - ModelState is Valid
2018-03-05 14:15:46.878 +00:00 [Information] Likvido.Website.Main.Controllers.HomeController: TEST INDEX LOGGER
2018-03-05 14:15:46.878 +00:00 [Information] Microsoft.AspNetCore.Mvc.ViewFeatures.Internal.ViewResultExecutor: Executing ViewResult, running view at path /Views/Home/Index.cshtml.
2018-03-05 14:15:46.878 +00:00 [Information] Microsoft.AspNetCore.Mvc.Internal.ControllerActionInvoker: Executed action Likvido.Website.Main.Controllers.HomeController.Index (Likvido.Website.Main) in 0.7351ms
2018-03-05 14:15:46.879 +00:00 [Information] Microsoft.AspNetCore.Hosting.Internal.WebHost: Request finished in 0.996ms 200 text/html; charset=utf-8
2018-03-05 14:15:47.518 +00:00 [Information] Microsoft.AspNetCore.Hosting.Internal.WebHost: Request starting HTTP/1.1 GET http://www.likvido.dk/js/site.min.js  
2018-03-05 14:15:47.520 +00:00 [Information] Microsoft.AspNetCore.Hosting.Internal.WebHost: Request finished in 1.6787ms 302 
2018-03-05 14:15:47.617 +00:00 [Information] Microsoft.AspNetCore.Hosting.Internal.WebHost: Request starting HTTP/1.1 GET http://likvido.dk/js/site.min.js  
2018-03-05 14:15:47.617 +00:00 [Information] Microsoft.AspNetCore.Hosting.Internal.WebHost: Request finished in 0.628ms 404 
10
Lars Holdgaard

Vous devez ajouter des fournisseurs comme mentionné plus loin dans https://docs.Microsoft.com/en-us/aspnet/core/fundamentals/logging/?tabs=aspnetcore2x :

public static void Main(string[] args)
{
    var webHost = new WebHostBuilder()
        .UseKestrel()
        .UseContentRoot(Directory.GetCurrentDirectory())
        .ConfigureAppConfiguration((hostingContext, config) =>
        {
            var env = hostingContext.HostingEnvironment;
            config.AddJsonFile("appsettings.json", optional: true, reloadOnChange: true)
                  .AddJsonFile($"appsettings.{env.EnvironmentName}.json", optional: true, reloadOnChange: true);
            config.AddEnvironmentVariables();
        })
        .ConfigureLogging((hostingContext, logging) =>
        {
            logging.AddConfiguration(hostingContext.Configuration.GetSection("Logging"));
            logging.AddConsole();
            logging.AddDebug();
        })
        .UseStartup<Startup>()
        .Build();

    webHost.Run();
}

Pour Azure, vous pouvez utiliser le fournisseur Azure App Service ( https://www.nuget.org/packages/Microsoft.Extensions.Logging.AzureAppServices )

logging.AddAzureWebAppDiagnostics();
2
Ojisah