2017-08-31 77 views
2

我給我的第一個步驟,對.NET核心登錄到與.net核2.0的Web應用程序

剛剛創建了一個網絡世界,你好有

dotnet new web 

我可以看到控制檯有某種日誌啓用。我只想將某些內容記錄到控制檯。

但我不知道如何從

app.Run(async (context) => 
{ 
    await context.Response.WriteAsync("Hello World!!!"); 
}); 

我爲Console.WriteLine嘗試,但它顯然沒有奏效訪問記錄。

也嘗試與NLog按照本指南https://github.com/NLog/NLog.Web/wiki/Getting-started-with-ASP.NET-Core-(csproj---vs2017)但我不知道如何注入記錄器。

我只是爲了教育目的而環顧四周,不尋找一個真正的記錄器,所以也許有一個更好/更容易的選擇。

+1

日誌記錄以及記錄在這裏的根https://開頭的文檔。 microsoft.com/en-us/aspnet/core/fundamentals/logging?tabs=aspnetcore2x – Jehof

回答

2

我可以這樣實現它:

[...] 
using Microsoft.Extensions.Logging; 
[...] 
namespace web 
{ 
    public class Startup 
    { 
     ILogger log; 
     public Startup(ILoggerFactory loggerFactory) 
     { 
      log = loggerFactory.CreateLogger("Logger"); 
     } 
     [...] 

     public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory) 
     { 
      loggerFactory.AddConsole(); 
      loggerFactory.AddDebug(); 

      [...] 

      app.Run(async (context) => 
      { 
       log.LogInformation("logging!"); 
       await context.Response.WriteAsync("Hello World!"); 
      }); 
     } 
    } 
} 

也有一個appsettings.json文件添加到項目

{ 
    "Logging": { 
    "IncludeScopes": false, 
    "LogLevel": { 
     "Default": "Debug", 
     "System": "Information", 
     "Microsoft": "Information" 
    } 
    } 
} 
+0

我在這一段時間對此感到震驚。原來默認的LogLevel被設置爲'Warning',並且我正在登錄'Information' ...感謝推送。 – JackMorrissey