Skip to content

Commit

Permalink
Updated Net5 example NLog.config to include logging of hosting lifeti…
Browse files Browse the repository at this point in the history
…me messages (#627)
  • Loading branch information
snakefoot authored Feb 14, 2021
1 parent 0197d43 commit 1acc578
Show file tree
Hide file tree
Showing 30 changed files with 64 additions and 58 deletions.
2 changes: 1 addition & 1 deletion NLog.Web.sln
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Examples", "Examples", "{A9
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ASP.NET 4.6.1 - VS2017", "examples\ASP.NET 4.6.1\Visual Studio 2017\ASP.NET 4.6.1 - VS2017\ASP.NET 4.6.1 - VS2017.csproj", "{0BF1FB4E-D5AE-46DE-9E80-E2DD40235552}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ASP.NET Core 3 - VS2019", "examples\ASP.NET Core 3\ASP.NET Core 3 - VS2019\ASP.NET Core 3 - VS2019.csproj", "{A0DF49D3-74E1-4FD2-8197-017A824B9AED}"
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "ASP.NET Core 3 - VS2019", "examples\ASP.NET Core 3\ASP.NET Core 3 - VS2019\ASP.NET Core 3 - VS2019.csproj", "{A0DF49D3-74E1-4FD2-8197-017A824B9AED}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@
<TargetFramework>netcoreapp2.1</TargetFramework>
<TargetLatestAspNetCoreRuntimePatch>true</TargetLatestAspNetCoreRuntimePatch>
<RuntimeFrameworkVersion>2.1.*</RuntimeFrameworkVersion>
<AssemblyName>NLog.Web.AspNetCore2.Example</AssemblyName>
<RootNamespace>NLog.Web.AspNetCore2.Example</RootNamespace>
<AssemblyName>ASP.NetCore2_NLog_Web_Example</AssemblyName>
<RootNamespace>ASP.NetCore2_NLog_Web_Example</RootNamespace>
<FileVersion>1.0.0.1</FileVersion>
<InformationalVersion>1.0.0.2</InformationalVersion>
</PropertyGroup>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@
using System.Linq;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Logging;
using NLog.Web.AspNetCore2.Example.Models;
using ASP.NetCore2_NLog_Web_Example.Models;
using NLog.Web.LayoutRenderers;

namespace NLog.Web.AspNetCore2.Example.Controllers
namespace ASP.NetCore2_NLog_Web_Example.Controllers
{
public class HomeController : Controller
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
using System;

namespace NLog.Web.AspNetCore2.Example.Models
namespace ASP.NetCore2_NLog_Web_Example.Models
{
public class ErrorViewModel
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
using System.Linq;
using System.Threading.Tasks;

namespace NLog.Web.AspNetCore2.Example.Models
namespace ASP.NetCore2_NLog_Web_Example.Models
{
public class MessageModel
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@

<!-- another file log, only own logs. Uses some ASP.NET core renderers -->
<target xsi:type="File" name="ownFile-web" fileName="c:\temp\nlog-AspNetCore2-own-${shortdate}.log"
layout="${longdate}|${event-properties:item=EventId_Id:whenEmpty=0}|${uppercase:${level}}|${logger}|${message} ${exception:format=tostring}|url: ${aspnet-request-url}|action: ${aspnet-mvc-action}|${callsite}" />
layout="${longdate}|${event-properties:item=EventId_Id:whenEmpty=0}|${uppercase:${level}}|${logger}|${message} ${exception:format=tostring}|url: ${aspnet-request-url}|action: ${aspnet-mvc-action}" />
</targets>

<!-- rules to map from logger name to target -->
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
using NLog.Extensions.Logging;
using NLog.Web;

namespace NLog.Web.AspNetCore2.Example
namespace ASP.NetCore2_NLog_Web_Example
{
public static class Program
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;

namespace NLog.Web.AspNetCore2.Example
namespace ASP.NetCore2_NLog_Web_Example
{
public class Startup
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
@model NLog.Web.AspNetCore2.Example.Models.MessageModel
@model ASP.NetCore2_NLog_Web_Example.Models.MessageModel

@{
ViewData["Title"] = "Contact";
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
@using NLog.Web.AspNetCore2.Example.Models
@model NLog.Web.AspNetCore2.Example.Models.ErrorViewModel
@using ASP.NetCore2_NLog_Web_Example.Models
@model ASP.NetCore2_NLog_Web_Example.Models.ErrorViewModel
@{
ViewData["Title"] = "Error";
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
<Project Sdk="Microsoft.NET.Sdk.Web">
<Project Sdk="Microsoft.NET.Sdk.Web">

<PropertyGroup>
<TargetFramework>netcoreapp3.0</TargetFramework>
<RootNamespace>ASP.NET_Core_3___VS2019</RootNamespace>
<TargetFramework>netcoreapp3.1</TargetFramework>
<AssemblyName>ASP.NetCore3_NLog_Web_Example</AssemblyName>
<RootNamespace>ASP.NetCore3_NLog_Web_Example</RootNamespace>
</PropertyGroup>

<ItemGroup>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Logging;

namespace ASP.NET_Core_3___VS2019.Controllers
namespace ASP.NetCore3_NLog_Web_Example.Controllers
{
[Route("api/[controller]")]
[ApiController]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Logging;
using ASP.NET_Core_3___VS2019.Models;
using ASP.NetCore3_NLog_Web_Example.Models;

namespace ASP.NET_Core_3___VS2019.Controllers
namespace ASP.NetCore3_NLog_Web_Example.Controllers
{
public class HomeController : Controller
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
using System;

namespace ASP.NET_Core_3___VS2019.Models
namespace ASP.NetCore3_NLog_Web_Example.Models
{
public class ErrorViewModel
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,11 @@
using System.Linq;
using System.Threading.Tasks;

namespace ASP.NET_Core_3___VS2019.Models
namespace ASP.NetCore3_NLog_Web_Example.Models
{
public class ItemModel
{
public string Name { get; set; }
public string Value { get; set; }

}
}
6 changes: 3 additions & 3 deletions examples/ASP.NET Core 3/ASP.NET Core 3 - VS2019/NLog.config
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@

<!-- File Target for own log messages with extra web details using some ASP.NET core renderers -->
<target xsi:type="File" name="ownFile-web" fileName="c:\temp\nlog-AspNetCore3-own-${shortdate}.log"
layout="${longdate}|${event-properties:item=EventId_Id:whenEmpty=0}|${uppercase:${level}}|${logger}|${message} ${exception:format=tostring}|url: ${aspnet-request-url}|action: ${aspnet-mvc-action}|${callsite}| body: ${aspnet-request-posted-body}" />
layout="${longdate}|${event-properties:item=EventId_Id:whenEmpty=0}|${uppercase:${level}}|${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 -->
<target xsi:type="Console" name="lifetimeConsole" layout="${level:truncate=4:tolower=true}\: ${logger}[0]${newline} ${message}${exception:format=tostring}" />
Expand All @@ -33,8 +33,8 @@
<!--Output hosting lifetime messages to console target for faster startup detection -->
<logger name="Microsoft.Hosting.Lifetime" minlevel="Info" writeTo="lifetimeConsole, ownFile-web" final="true" />

<!--Skip non-critical Microsoft logs and so log only own logs-->
<logger name="Microsoft.*" maxlevel="Info" final="true" /> <!-- BlackHole -->
<!--Skip non-critical Microsoft logs and so log only own logs (BlackHole) -->
<logger name="Microsoft.*" maxlevel="Info" final="true" />

<logger name="*" minlevel="Trace" writeTo="ownFile-web" />
</rules>
Expand Down
2 changes: 1 addition & 1 deletion examples/ASP.NET Core 3/ASP.NET Core 3 - VS2019/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
using NLog;
using NLog.Web;

namespace ASP.NET_Core_3___VS2019
namespace ASP.NetCore3_NLog_Web_Example
{
public class Program
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
"ASPNETCORE_ENVIRONMENT": "Development"
}
},
"ASP.NET_Core_3___VS2019": {
"ASP.NetCore3_NLog_Web_Example": {
"commandName": "Project",
"launchBrowser": true,
"applicationUrl": "https://localhost:5001;http://localhost:5000",
Expand Down
2 changes: 1 addition & 1 deletion examples/ASP.NET Core 3/ASP.NET Core 3 - VS2019/Startup.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;

namespace ASP.NET_Core_3___VS2019
namespace ASP.NetCore3_NLog_Web_Example
{
public class Startup
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,15 @@
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>@ViewData["Title"] - ASP.NET_Core_3___VS2019</title>
<title>@ViewData["Title"] - ASP.NetCore3_NLog_Web_Example</title>
<link rel="stylesheet" href="~/lib/bootstrap/dist/css/bootstrap.min.css" />
<link rel="stylesheet" href="~/css/site.css" />
</head>
<body>
<header>
<nav class="navbar navbar-expand-sm navbar-toggleable-sm navbar-light bg-white border-bottom box-shadow mb-3">
<div class="container">
<a class="navbar-brand" asp-area="" asp-controller="Home" asp-action="Index">ASP.NET_Core_3___VS2019</a>
<a class="navbar-brand" asp-area="" asp-controller="Home" asp-action="Index">ASP.NetCore3_NLog_Web_Example</a>
<button class="navbar-toggler" type="button" data-toggle="collapse" data-target=".navbar-collapse" aria-controls="navbarSupportedContent"
aria-expanded="false" aria-label="Toggle navigation">
<span class="navbar-toggler-icon"></span>
Expand All @@ -37,7 +37,7 @@

<footer class="border-top footer text-muted">
<div class="container">
&copy; 2019 - ASP.NET_Core_3___VS2019 - <a asp-area="" asp-controller="Home" asp-action="Privacy">Privacy</a>
&copy; 2019 - ASP.NetCore3_NLog_Web_Example - <a asp-area="" asp-controller="Home" asp-action="Privacy">Privacy</a>
</div>
</footer>
<script src="~/lib/jquery/dist/jquery.min.js"></script>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
@using ASP.NET_Core_3___VS2019
@using ASP.NET_Core_3___VS2019.Models
@using ASP.NetCore3_NLog_Web_Example
@using ASP.NetCore3_NLog_Web_Example.Models
@addTagHelper *, Microsoft.AspNetCore.Mvc.TagHelpers
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
<Project Sdk="Microsoft.NET.Sdk.Web">
<Project Sdk="Microsoft.NET.Sdk.Web">

<PropertyGroup>
<TargetFramework>net5.0</TargetFramework>
<RootNamespace>ASP.NET_Core_5_NLog_Example</RootNamespace>
<AssemblyName>ASP.NetCore5_NLog_Web_Example</AssemblyName>
<RootNamespace>ASP.NetCore5_NLog_Web_Example</RootNamespace>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="NLog.Web.AspNetCore" Version="4.9.3" />
<PackageReference Include="NLog" Version="4.7.6" />
<ProjectReference Include="..\..\..\src\NLog.Web.AspNetCore\NLog.Web.AspNetCore.csproj" />
</ItemGroup>

</Project>
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using ASP.NET_Core_5_NLog_Example.Models;
using ASP.NetCore5_NLog_Web_Example.Models;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Logging;
using System;
Expand All @@ -7,7 +7,7 @@
using System.Linq;
using System.Threading.Tasks;

namespace ASP.NET_Core_5_NLog_Example.Controllers
namespace ASP.NetCore5_NLog_Web_Example.Controllers
{
public class HomeController : Controller
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
using System;

namespace ASP.NET_Core_5_NLog_Example.Models
namespace ASP.NetCore5_NLog_Web_Example.Models
{
public class ErrorViewModel
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
using System;
using NLog.Web;

namespace ASP.NET_Core_5_NLog_Example
namespace ASP.NetCore5_NLog_Web_Example
{
public class Program
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
"ASPNETCORE_ENVIRONMENT": "Development"
}
},
"ASP.NET_Core_5_NLog_Example": {
"ASP.NetCore5_NLog_Web_Example": {
"commandName": "Project",
"dotnetRunMessages": "true",
"launchBrowser": true,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
using System.Linq;
using System.Threading.Tasks;

namespace ASP.NET_Core_5_NLog_Example
namespace ASP.NetCore5_NLog_Web_Example
{
public class Startup
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,15 @@
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>@ViewData["Title"] - ASP.NET_Core_5_NLog_Example</title>
<title>@ViewData["Title"] - ASP.NetCore5_NLog_Web_Example</title>
<link rel="stylesheet" href="~/lib/bootstrap/dist/css/bootstrap.min.css" />
<link rel="stylesheet" href="~/css/site.css" />
</head>
<body>
<header>
<nav class="navbar navbar-expand-sm navbar-toggleable-sm navbar-light bg-white border-bottom box-shadow mb-3">
<div class="container">
<a class="navbar-brand" asp-area="" asp-controller="Home" asp-action="Index">ASP.NET_Core_5_NLog_Example</a>
<a class="navbar-brand" asp-area="" asp-controller="Home" asp-action="Index">ASP.NetCore5_NLog_Web_Example</a>
<button class="navbar-toggler" type="button" data-toggle="collapse" data-target=".navbar-collapse" aria-controls="navbarSupportedContent"
aria-expanded="false" aria-label="Toggle navigation">
<span class="navbar-toggler-icon"></span>
Expand All @@ -37,7 +37,7 @@

<footer class="border-top footer text-muted">
<div class="container">
&copy; 2020 - ASP.NET_Core_5_NLog_Example - <a asp-area="" asp-controller="Home" asp-action="Privacy">Privacy</a>
&copy; 2020 - ASP.NetCore5_NLog_Web_Example - <a asp-area="" asp-controller="Home" asp-action="Privacy">Privacy</a>
</div>
</footer>
<script src="~/lib/jquery/dist/jquery.min.js"></script>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
@using ASP.NET_Core_5_NLog_Example
@using ASP.NET_Core_5_NLog_Example.Models
@using ASP.NetCore5_NLog_Web_Example
@using ASP.NetCore5_NLog_Web_Example.Models
@addTagHelper *, Microsoft.AspNetCore.Mvc.TagHelpers
26 changes: 16 additions & 10 deletions examples/ASP.NET Core 5/ASP.NET Core 5 NLog Example/nlog.config
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
autoReload="true"
internalLogLevel="Info"
internalLogFile="c:\temp\internal-nlog.txt">
internalLogFile="c:\temp\internal-nlog-AspNetCore5.txt">

<!-- enable asp.net core layout renderers -->
<extensions>
Expand All @@ -12,24 +12,30 @@

<!-- the targets to write to -->
<targets>
<!-- write logs to file -->
<target xsi:type="File" name="allfile" fileName="c:\temp\nlog-all-${shortdate}.log"
layout="${longdate}|${event-properties:item=EventId_Id}|${uppercase:${level}}|${logger}|${message} ${exception:format=tostring}" />
<!-- File Target for all log messages with basic details -->
<target xsi:type="File" name="allfile" fileName="c:\temp\nlog-AspNetCore5-all-${shortdate}.log"
layout="${longdate}|${event-properties:item=EventId_Id:whenEmpty=0}|${uppercase:${level}}|${logger}|${message} ${exception:format=tostring}" />

<!-- another file log, only own logs. Uses some ASP.NET core renderers -->
<target xsi:type="File" name="ownFile-web" fileName="c:\temp\nlog-own-${shortdate}.log"
layout="${longdate}|${event-properties:item=EventId_Id}|${uppercase:${level}}|${logger}|${message} ${exception:format=tostring}|url: ${aspnet-request-url}|action: ${aspnet-mvc-action}" />
<!-- File Target for own log messages with extra web details using some ASP.NET core renderers -->
<target xsi:type="File" name="ownFile-web" fileName="c:\temp\nlog-AspNetCore5-own-${shortdate}.log"
layout="${longdate}|${event-properties:item=EventId_Id:whenEmpty=0}|${uppercase:${level}}|${logger}|${message} ${exception:format=tostring}|url: ${aspnet-request-url}|action: ${aspnet-mvc-action}|${callsite}| body: ${aspnet-request-posted-body}" />

<!--Console Target for hosting lifetime messages to improve Docker / Visual Studio startup detection -->
<target xsi:type="Console" name="lifetimeConsole" layout="${level:truncate=4:tolower=true}\: ${logger}[0]${newline} ${message}${exception:format=tostring}" />
</targets>

<!-- rules to map from logger name to target -->
<rules>
<!--All logs, including from Microsoft-->
<logger name="*" minlevel="Trace" writeTo="allfile" />

<!--Skip non-critical Microsoft logs and so log only own logs-->
<logger name="Microsoft.*" maxlevel="Info" final="true" /> <!-- BlackHole without writeTo -->
<logger name="System.Net.Http.*" maxlevel="Info" final="true" /> <!-- BlackHole without writeTo -->
<!--Output hosting lifetime messages to console target for faster startup detection -->
<logger name="Microsoft.Hosting.Lifetime" minlevel="Info" writeTo="lifetimeConsole, ownFile-web" final="true" />

<!--Skip non-critical Microsoft logs and so log only own logs (BlackHole) -->
<logger name="Microsoft.*" maxlevel="Info" final="true" />
<logger name="System.Net.Http.*" maxlevel="Info" final="true" />

<logger name="*" minlevel="Trace" writeTo="ownFile-web" />
</rules>
</nlog>

0 comments on commit 1acc578

Please sign in to comment.