2016-12-22 3 views
16

Um eine .net Core-App zu debuggen, die beim Start fehlschlägt, möchte ich Protokolle aus der startup.cs-Datei schreiben. Ich habe Logging-Setup innerhalb der Datei, die im Rest der App außerhalb der Datei startup.cs verwendet werden kann, aber nicht sicher, wie Protokolle aus der Datei startup.cs selbst zu schreiben.Wie schreibe ich Protokolle aus Startup.cs

Antwort

12

Option 1: direkt verwenden log (zB Serilog) in startup

public class Startup 
{ 
    public Startup(IHostingEnvironment env) 
    { 
     Log.Logger = new LoggerConfiguration() 
      .MinimumLevel.Debug() 
      .WriteTo.RollingFile(Path.Combine(env.ContentRootPath, "Serilog-{Date}.txt")) 
      .CreateLogger(); 

     Log.Information("Inside Startup ctor"); 
     .... 
    } 

    public void ConfigureServices(IServiceCollection services) 
    { 
     Log.Information("ConfigureServices"); 
     .... 
    } 

    public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory) 
    { 
     Log.Information("Configure"); 
     .... 
    } 

Output:

serilog

Zur Einrichtung Serilog in asp.net-Core-Anwendung , schau dir die Serilog.AspNetCore package on GitHub an.


Option2: Konfigurieren der Protokollierung in Program.cs wie this-

var host = new WebHostBuilder() 
      .UseKestrel() 
      .ConfigureServices(s => { 
       s.AddSingleton<IFormatter, LowercaseFormatter>(); 
      }) 
      .ConfigureLogging(f => f.AddConsole(LogLevel.Debug)) 
      .UseStartup<Startup>() 
      .Build(); 

host.Run(); 

Benutzer loggerFactory in Start wie this-

public class Startup 
{ 
    ILogger _logger; 
    IFormatter _formatter; 
    public Startup(ILoggerFactory loggerFactory, IFormatter formatter) 
    { 
     _logger = loggerFactory.CreateLogger<Startup>(); 
     _formatter = formatter; 
    } 

    public void ConfigureServices(IServiceCollection services) 
    { 
     _logger.LogDebug($"Total Services Initially: {services.Count}"); 

     // register services 
     //services.AddSingleton<IFoo, Foo>(); 
    } 

    public void Configure(IApplicationBuilder app, IFormatter formatter) 
    { 
     // note: can request IFormatter here as well as via constructor 
     _logger.LogDebug("Configure() started..."); 
     app.Run(async (context) => await context.Response.WriteAsync(_formatter.Format("Hi!"))); 
     _logger.LogDebug("Configure() complete."); 
    } 
} 

Vollständige Angaben auf dieser link

0

Ich habe es geschafft, dies zu tun, indem Sie einen Logger mit Nlog in der Datei statisch erstellen, dann verwenden Sie dies innerhalb der Startmethoden.

private readonly NLog.Logger _logger = new NLog.LogFactory().GetCurrentClassLogger(); 
19

Dies hat sich geändert s deutlich mit der Veröffentlichung von ASP.NET Core 2.0. In ASP.NET Core 2.0 wird die Protokollierung im Host-Generator erstellt. Dies bedeutet, dass die Protokollierung standardmäßig über DI verfügbar ist und in die Klasse Startup

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

    public IConfiguration Configuration { get; } 

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

    public void ConfigureServices(IServiceCollection services) 
    { 
     _logger.LogInformation("ConfigureServices called"); 

     // … 
    } 

    public void Configure(IApplicationBuilder app, IHostingEnvironment env) 
    { 
     _logger.LogInformation("Configure called"); 

     // … 
    } 
} 
eingegeben werden kann
Verwandte Themen