- Notifications
You must be signed in to change notification settings - Fork1.4k
Getting started with ASP.NET Core 3
ℹ️ See alsoexample in GitHub
This tutorial is for ASP.NET Core 3.1
- For ASP.NET Core 6, checkGetting started with ASP.NET Core 6
In Visual Studio 2019.
Install the latest:
- NLog.Web.AspNetCore 4.9+
- Update theNLog package if possible
in csproj:
<PackageReferenceInclude="NLog.Web.AspNetCore"Version="4.*" /><PackageReferenceInclude="NLog"Version="4.*" />
Create nlog.config (lowercase all) file in the root of your project.
We use this example:
<?xml version="1.0" encoding="utf-8" ?><nlogxmlns="http://www.nlog-project.org/schemas/NLog.xsd"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://www.nlog-project.org/schemas/NLog.xsd NLog.xsd"> autoReload="true" throwConfigExceptions="true" internalLogLevel="info" internalLogFile="c:\temp\internal-nlog-AspNetCore3.txt"><!-- enable asp.net core layout renderers--> <extensions> <addassembly="NLog.Web.AspNetCore"/> </extensions><!-- the targets to write to--> <targets><!-- File Target for all log messages with basic details--> <targetxsi:type="File"name="allfile"fileName="c:\temp\nlog-AspNetCore3-all-${shortdate}.log"layout="${longdate}|${event-properties:item=EventId_Id:whenEmpty=0}|${level:uppercase=true}|${logger}|${message} ${exception:format=tostring}" /><!-- File Target for own log messages with extra web details using some ASP.NET core renderers--> <targetxsi:type="File"name="ownFile-web"fileName="c:\temp\nlog-AspNetCore3-own-${shortdate}.log"layout="${longdate}|${event-properties:item=EventId_Id:whenEmpty=0}|${level:uppercase=true}|${logger}|${message} ${exception:format=tostring}|url: ${aspnet-request-url}|action: ${aspnet-mvc-action}|" /><!--Console Target for hosting lifetime messages to improve Docker / Visual Studio startup detection--> <targetxsi:type="Console"name="lifetimeConsole"layout="${MicrosoftConsoleLayout}" /> </targets><!-- rules to map from logger name to target--> <rules><!--All logs, including from Microsoft--> <loggername="*"minlevel="Trace"writeTo="allfile" /><!--Output hosting lifetime messages to console target for faster startup detection--> <loggername="Microsoft.Hosting.Lifetime"minlevel="Info"writeTo="lifetimeConsole, ownFile-web"final="true" /><!--Skip non-critical Microsoft logs and so log only own logs--> <loggername="Microsoft.*"maxlevel="Info"final="true" /><!-- BlackHole--> <loggername="*"minlevel="Trace"writeTo="ownFile-web" /> </rules></nlog>
More details of the config file arehere.
Notice that one might have to pay special attention to theHosting Lifetime Startup Messages, if removing all other LoggingProviders (Like Console) and only using NLog. As it can cause hosting environment (Visual Studio / Docker / Azure Container) to not see application as started.
If you like to include other targets or layout renderers, check thePlatform support table, as there is a limited set implemented. Check the columnNetStandard
. To read more about NetStandard, seethe docs from Microsoft. Check .NET Standard 2 for ASP.NET Core 3.
Enable copy to bin folder for nlog.config
or edit.csproj
file manually and add:
<ItemGroup> <ContentUpdate="nlog.config"CopyToOutputDirectory="PreserveNewest" /> </ItemGroup>
Update the program.cs to includeUseNLog()
:
usingSystem;usingNLog.Web;usingMicrosoft.AspNetCore;usingMicrosoft.AspNetCore.Hosting;usingMicrosoft.Extensions.Logging;usingMicrosoft.Extensions.Hosting;publicstaticvoidMain(string[]args){// Early init of NLog to allow startup and exception logging, before host is builtvarlogger=NLog.LogManager.Setup().LoadConfigurationFromAppSettings().GetCurrentClassLogger();try{logger.Debug("init main");CreateHostBuilder(args).Build().Run();}catch(Exceptionexception){//NLog: catch setup errorslogger.Error(exception,"Stopped program because of exception");throw;}finally{// Ensure to flush and stop internal timers/threads before application-exit (Avoid segmentation fault on Linux)NLog.LogManager.Shutdown();}}publicstaticIHostBuilderCreateHostBuilder(string[]args)=>Host.CreateDefaultBuilder(args).ConfigureWebHostDefaults(webBuilder=>{webBuilder.UseStartup<Startup>();}).ConfigureLogging(logging=>{logging.ClearProviders();logging.SetMinimumLevel(Microsoft.Extensions.Logging.LogLevel.Trace);}).UseNLog();// NLog: Setup NLog for Dependency injection
The Logging configuration specified inappsettings.json
overrides any call toSetMinimumLevel
. So either remove"Default":
or adjust it correctly to your needs.
{"Logging": {"LogLevel": {"Default":"Trace","Microsoft":"Warning","Microsoft.Hosting.Lifetime":"Information" } },"AllowedHosts":"*"}
Remember to also update any environment specific configuration to avoid any surprises. Exappsettings.Development.json
Inject the ILogger in your controller:
usingMicrosoft.Extensions.Logging;publicclassHomeController:Controller{privatereadonlyILogger<HomeController>_logger;publicHomeController(ILogger<HomeController>logger){_logger=logger;_logger.LogDebug(1,"NLog injected into HomeController");}publicIActionResultIndex(){_logger.LogInformation("Hello, this is the index!");returnView();}
When starting the ASP.NET Core website, we get two files:
2019-10-14 23:15:09.3898|0|DEBUG|ASP.NET_Core_3___VS2019.Program|init main |url: |action: |ASP.NET_Core_3___VS2019.Program.Main2019-10-14 23:15:11.3867|1|DEBUG|ASP.NET_Core_3___VS2019.Controllers.HomeController|NLog injected into HomeController |url: https://localhost/|action: Index|ASP.NET_Core_3___VS2019.Controllers.HomeController..ctor2019-10-14 23:15:11.3867|0|INFO|ASP.NET_Core_3___VS2019.Controllers.HomeController|Hello, this is the index! |url: https://localhost/|action: Index|ASP.NET_Core_3___VS2019.Controllers.HomeController.Index
2019-10-14 23:15:09.3898|0|DEBUG|ASP.NET_Core_3___VS2019.Program|init main 2019-10-14 23:15:10.9142|0|INFO|Microsoft.AspNetCore.DataProtection.KeyManagement.XmlKeyManager|User profile is available. Using 'C:\Users\Julian\AppData\Local\ASP.NET\DataProtection-Keys' as key repository and Windows DPAPI to encrypt keys at rest. 2019-10-14 23:15:11.2680|0|INFO|Microsoft.Hosting.Lifetime|Application started. Press Ctrl+C to shut down. 2019-10-14 23:15:11.2680|0|INFO|Microsoft.Hosting.Lifetime|Hosting environment: Development 2019-10-14 23:15:11.2680|0|INFO|Microsoft.Hosting.Lifetime|Content root path: D:\nlog\NLog.Web\examples\ASP.NET Core 3\ASP.NET Core 3 - VS2019 2019-10-14 23:15:11.3238|1|INFO|Microsoft.AspNetCore.Hosting.Diagnostics|Request starting HTTP/2.0 GET https://localhost:44318/ 2019-10-14 23:15:11.3434|0|INFO|Microsoft.AspNetCore.Routing.EndpointMiddleware|Executing endpoint 'ASP.NET_Core_3___VS2019.Controllers.HomeController.Index (ASP.NET Core 3 - VS2019)' 2019-10-14 23:15:11.3867|3|INFO|Microsoft.AspNetCore.Mvc.Infrastructure.ControllerActionInvoker|Route matched with {action = "Index", controller = "Home"}. Executing controller action with signature Microsoft.AspNetCore.Mvc.IActionResult Index() on controller ASP.NET_Core_3___VS2019.Controllers.HomeController (ASP.NET Core 3 - VS2019). 2019-10-14 23:15:11.3867|1|DEBUG|ASP.NET_Core_3___VS2019.Controllers.HomeController|NLog injected into HomeController 2019-10-14 23:15:11.3867|0|INFO|ASP.NET_Core_3___VS2019.Controllers.HomeController|Hello, this is the index! 2019-10-14 23:15:11.4161|1|INFO|Microsoft.AspNetCore.Mvc.ViewFeatures.ViewResultExecutor|Executing ViewResult, running view Index. 2019-10-14 23:15:11.5831|4|INFO|Microsoft.AspNetCore.Mvc.ViewFeatures.ViewResultExecutor|Executed ViewResult - view Index executed in 167.626ms. 2019-10-14 23:15:11.5831|2|INFO|Microsoft.AspNetCore.Mvc.Infrastructure.ControllerActionInvoker|Executed action ASP.NET_Core_3___VS2019.Controllers.HomeController.Index (ASP.NET Core 3 - VS2019) in 196.6052ms 2019-10-14 23:15:11.5831|1|INFO|Microsoft.AspNetCore.Routing.EndpointMiddleware|Executed endpoint 'ASP.NET_Core_3___VS2019.Controllers.HomeController.Index (ASP.NET Core 3 - VS2019)' 2019-10-14 23:15:11.5910|2|INFO|Microsoft.AspNetCore.Hosting.Diagnostics|Request finished in 267.9366ms 200 text/html; charset=utf-8 2019-10-14 23:15:11.6171|1|INFO|Microsoft.AspNetCore.Hosting.Diagnostics|Request starting HTTP/2.0 GET https://localhost:44318/lib/jquery/dist/jquery.min.js 2019-10-14 23:15:11.6171|1|INFO|Microsoft.AspNetCore.Hosting.Diagnostics|Request starting HTTP/2.0 GET https://localhost:44318/css/site.css 2019-10-14 23:15:11.6171|1|INFO|Microsoft.AspNetCore.Hosting.Diagnostics|Request starting HTTP/2.0 GET https://localhost:44318/lib/bootstrap/dist/css/bootstrap.min.css 2019-10-14 23:15:11.6365|1|INFO|Microsoft.AspNetCore.Hosting.Diagnostics|Request starting HTTP/2.0 GET https://localhost:44318/lib/bootstrap/dist/js/bootstrap.bundle.min.js 2019-10-14 23:15:11.6365|1|INFO|Microsoft.AspNetCore.Hosting.Diagnostics|Request starting HTTP/2.0 GET https://localhost:44318/js/site.js?v=4q1jwFhaPaZgr8WAUSrux6hAuh0XDg9kPS3xIVq36I0 2019-10-14 23:15:11.6627|2|INFO|Microsoft.AspNetCore.StaticFiles.StaticFileMiddleware|Sending file. Request path: '/js/site.js'. Physical path: 'D:\nlog\NLog.Web\examples\ASP.NET Core 3\ASP.NET Core 3 - VS2019\wwwroot\js\site.js' 2019-10-14 23:15:11.6627|2|INFO|Microsoft.AspNetCore.StaticFiles.StaticFileMiddleware|Sending file. Request path: '/css/site.css'. Physical path: 'D:\nlog\NLog.Web\examples\ASP.NET Core 3\ASP.NET Core 3 - VS2019\wwwroot\css\site.css' 2019-10-14 23:15:11.6627|2|INFO|Microsoft.AspNetCore.Hosting.Diagnostics|Request finished in 28.7787ms 200 application/javascript 2019-10-14 23:15:11.6696|2|INFO|Microsoft.AspNetCore.Hosting.Diagnostics|Request finished in 52.4825ms 200 text/css 2019-10-14 23:15:11.6853|2|INFO|Microsoft.AspNetCore.StaticFiles.StaticFileMiddleware|Sending file. Request path: '/lib/bootstrap/dist/js/bootstrap.bundle.min.js'. Physical path: 'D:\nlog\NLog.Web\examples\ASP.NET Core 3\ASP.NET Core 3 - VS2019\wwwroot\lib\bootstrap\dist\js\bootstrap.bundle.min.js' 2019-10-14 23:15:11.6853|2|INFO|Microsoft.AspNetCore.StaticFiles.StaticFileMiddleware|Sending file. Request path: '/lib/bootstrap/dist/css/bootstrap.min.css'. Physical path: 'D:\nlog\NLog.Web\examples\ASP.NET Core 3\ASP.NET Core 3 - VS2019\wwwroot\lib\bootstrap\dist\css\bootstrap.min.css' 2019-10-14 23:15:11.6853|2|INFO|Microsoft.AspNetCore.StaticFiles.StaticFileMiddleware|Sending file. Request path: '/lib/jquery/dist/jquery.min.js'. Physical path: 'D:\nlog\NLog.Web\examples\ASP.NET Core 3\ASP.NET Core 3 - VS2019\wwwroot\lib\jquery\dist\jquery.min.js' 2019-10-14 23:15:11.6853|2|INFO|Microsoft.AspNetCore.Hosting.Diagnostics|Request finished in 50.937400000000004ms 200 application/javascript 2019-10-14 23:15:11.6853|2|INFO|Microsoft.AspNetCore.Hosting.Diagnostics|Request finished in 70.65100000000001ms 200 text/css 2019-10-14 23:15:11.6853|2|INFO|Microsoft.AspNetCore.Hosting.Diagnostics|Request finished in 71.51610000000001ms 200 application/javascript
Next step, seeConfigure NLog with nlog.config
-Troubleshooting Guide - See available NLog Targets and Layouts:https://nlog-project.org/config
- All targets, layouts and layout renderers
Popular: - Using NLog with NLog.config
- Using NLog with appsettings.json