-
Notifications
You must be signed in to change notification settings - Fork 165
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Added NLogBuilder and ASP.NET Core 2 example #191
Merged
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
#if NETSTANDARD2_0 | ||
|
||
|
||
using System; | ||
using System.Collections.Generic; | ||
using System.Reflection; | ||
using NLog.Common; | ||
using NLog.Config; | ||
|
||
namespace NLog.Web | ||
{ | ||
|
||
|
||
/// <summary> | ||
/// NLog helper for ASP.NET Standard 2 | ||
/// </summary> | ||
public static class NLogBuilder | ||
{ | ||
/// <summary> | ||
/// Register NLog.Web.AspNetCore, so so <extension> in NLog's config isn't needed. | ||
/// </summary> | ||
internal static void RegisterNLogWebAspNetCore() | ||
{ | ||
try | ||
{ | ||
InternalLogger.Debug("Registering NLog.Web.AspNetCore"); | ||
ConfigurationItemFactory.Default.RegisterItemsFromAssembly(typeof(AspNetExtensions).GetTypeInfo().Assembly); | ||
} | ||
catch (Exception ex) | ||
{ | ||
InternalLogger.Error(ex, "Registering NLog.Web.AspNetCore failed"); | ||
} | ||
|
||
} | ||
|
||
/// <summary> | ||
/// Configure NLog | ||
/// </summary> | ||
/// <param name="fileName">Path to NLog configuration file, e.g. nlog.config. </param>> | ||
/// <returns>LogFactory to get loggers, add events etc</returns> | ||
public static LogFactory ConfigureNLog(string fileName) | ||
{ | ||
RegisterNLogWebAspNetCore(); | ||
return ConfigureNLog(new XmlLoggingConfiguration(fileName)); | ||
} | ||
|
||
/// <summary> | ||
/// Configure NLog | ||
/// </summary> | ||
/// <param name="configuration"></param> | ||
/// <returns>LogFactory to get loggers, add events etc</returns> | ||
public static LogFactory ConfigureNLog(LoggingConfiguration configuration) | ||
{ | ||
RegisterNLogWebAspNetCore(); | ||
LogManager.Configuration = configuration; | ||
return LogManager.LogFactory; | ||
} | ||
} | ||
} | ||
#endif |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
3 changes: 3 additions & 0 deletions
3
examples/ASP.NET Core 2/Visual Studio 2017/ASP.NET Core 2 - VS2017/.bowerrc
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
{ | ||
"directory": "wwwroot/lib" | ||
} |
25 changes: 25 additions & 0 deletions
25
.../ASP.NET Core 2/Visual Studio 2017/ASP.NET Core 2 - VS2017/ASP.NET Core 2 - VS2017.csproj
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
<Project Sdk="Microsoft.NET.Sdk.Web"> | ||
|
||
<PropertyGroup> | ||
<TargetFramework>netcoreapp2.0</TargetFramework> | ||
</PropertyGroup> | ||
|
||
<ItemGroup> | ||
<PackageReference Include="Microsoft.AspNetCore.All" Version="2.0.0" /> | ||
</ItemGroup> | ||
|
||
<ItemGroup> | ||
<DotNetCliToolReference Include="Microsoft.VisualStudio.Web.CodeGeneration.Tools" Version="2.0.0" /> | ||
</ItemGroup> | ||
|
||
<ItemGroup> | ||
<ProjectReference Include="..\..\..\..\NLog.Web.AspNetCore\NLog.Web.AspNetCore.csproj" /> | ||
</ItemGroup> | ||
|
||
<ItemGroup> | ||
<Content Update="NLog.config"> | ||
<CopyToOutputDirectory>Always</CopyToOutputDirectory> | ||
</Content> | ||
</ItemGroup> | ||
|
||
</Project> |
49 changes: 49 additions & 0 deletions
49
...s/ASP.NET Core 2/Visual Studio 2017/ASP.NET Core 2 - VS2017/Controllers/HomeController.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Diagnostics; | ||
using System.Linq; | ||
using System.Threading.Tasks; | ||
using Microsoft.AspNetCore.Mvc; | ||
using ASP.NET_Core_2___VS2017.Models; | ||
using Microsoft.Extensions.Logging; | ||
|
||
namespace ASP.NET_Core_2___VS2017.Controllers | ||
{ | ||
public class HomeController : Controller | ||
{ | ||
private readonly ILogger<HomeController> _logger; | ||
|
||
//NLog: inject logger | ||
public HomeController(ILogger<HomeController> logger) | ||
{ | ||
|
||
_logger = logger; | ||
} | ||
|
||
public IActionResult Index() | ||
{ | ||
_logger.LogInformation("Hello, this is the index!"); | ||
return View(); | ||
} | ||
|
||
public IActionResult About() | ||
{ | ||
ViewData["Message"] = "Your application description page."; | ||
|
||
return View(); | ||
} | ||
|
||
public IActionResult Contact() | ||
{ | ||
ViewData["Message"] = "Your contact page."; | ||
|
||
return View(); | ||
} | ||
|
||
public IActionResult Error() | ||
{ | ||
_logger.LogError("Ow noos! an error"); | ||
return View(new ErrorViewModel { RequestId = Activity.Current?.Id ?? HttpContext.TraceIdentifier }); | ||
} | ||
} | ||
} |
11 changes: 11 additions & 0 deletions
11
examples/ASP.NET Core 2/Visual Studio 2017/ASP.NET Core 2 - VS2017/Models/ErrorViewModel.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
using System; | ||
|
||
namespace ASP.NET_Core_2___VS2017.Models | ||
{ | ||
public class ErrorViewModel | ||
{ | ||
public string RequestId { get; set; } | ||
|
||
public bool ShowRequestId => !string.IsNullOrEmpty(RequestId); | ||
} | ||
} |
32 changes: 32 additions & 0 deletions
32
examples/ASP.NET Core 2/Visual Studio 2017/ASP.NET Core 2 - VS2017/NLog.config
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
<?xml version="1.0" encoding="utf-8" ?> | ||
<nlog xmlns="http://www.nlog-project.org/schemas/NLog.xsd" | ||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | ||
autoReload="true" | ||
internalLogLevel="info" | ||
internalLogFile="c:\temp\internal-nlog.txt"> | ||
|
||
|
||
<!-- 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}" /> | ||
|
||
<!-- 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}|url: ${aspnet-request-url}|action: ${aspnet-mvc-action}" /> | ||
|
||
<!-- write to the void aka just remove --> | ||
<target xsi:type="Null" name="blackhole" /> | ||
</targets> | ||
|
||
<!-- rules to map from logger name to target --> | ||
<rules> | ||
<!--All logs, including from Microsoft--> | ||
<logger name="*" minlevel="Trace" writeTo="allfile" /> | ||
|
||
<!--Skip Microsoft logs and so log only own logs--> | ||
<logger name="Microsoft.*" minlevel="Trace" writeTo="blackhole" final="true" /> | ||
<logger name="*" minlevel="Trace" writeTo="ownFile-web" /> | ||
</rules> | ||
</nlog> |
41 changes: 41 additions & 0 deletions
41
examples/ASP.NET Core 2/Visual Studio 2017/ASP.NET Core 2 - VS2017/Program.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.IO; | ||
using System.Linq; | ||
using System.Threading.Tasks; | ||
using Microsoft.AspNetCore; | ||
using Microsoft.AspNetCore.Hosting; | ||
using Microsoft.Extensions.Configuration; | ||
using Microsoft.Extensions.Logging; | ||
using NLog; | ||
using NLog.Config; | ||
using NLog.Web; | ||
|
||
namespace ASP.NET_Core_2___VS2017 | ||
{ | ||
public class Program | ||
{ | ||
public static void Main(string[] args) | ||
{ | ||
// NLog: setup the logger first to catch all errors | ||
var logger = NLogBuilder.ConfigureNLog("NLog.config").GetCurrentClassLogger(); | ||
try | ||
{ | ||
logger.Debug("init main"); | ||
BuildWebHost(args).Run(); | ||
} | ||
catch (Exception e) | ||
{ | ||
//NLog: catch setup errors | ||
logger.Error(e, "Stopped program because of exception"); | ||
throw; | ||
} | ||
} | ||
|
||
public static IWebHost BuildWebHost(string[] args) => | ||
WebHost.CreateDefaultBuilder(args) | ||
.UseStartup<Startup>() | ||
.UseNLog() // NLog: setup NLog for Dependency injection | ||
.Build(); | ||
} | ||
} |
27 changes: 27 additions & 0 deletions
27
.../ASP.NET Core 2/Visual Studio 2017/ASP.NET Core 2 - VS2017/Properties/launchSettings.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
{ | ||
"iisSettings": { | ||
"windowsAuthentication": false, | ||
"anonymousAuthentication": true, | ||
"iisExpress": { | ||
"applicationUrl": "http://localhost:56152/", | ||
"sslPort": 0 | ||
} | ||
}, | ||
"profiles": { | ||
"IIS Express": { | ||
"commandName": "IISExpress", | ||
"launchBrowser": true, | ||
"environmentVariables": { | ||
"ASPNETCORE_ENVIRONMENT": "Development" | ||
} | ||
}, | ||
"ASP.NET_Core_2___VS2017": { | ||
"commandName": "Project", | ||
"launchBrowser": true, | ||
"environmentVariables": { | ||
"ASPNETCORE_ENVIRONMENT": "Development" | ||
}, | ||
"applicationUrl": "http://localhost:56153/" | ||
} | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@snakefoot updated it! I think is indeed a lot better.
@grokky1 any opinion on this? Do you think this is clear?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The latest changes are good. Looking forward to using this.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@grokky1 the beta is live. Please let me know of you're happy, rtm will be soon then. See also.https://github.com/NLog/NLog.Web/wiki/Getting-started-with-ASP.NET-Core-2
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@304NotModified Sorry busy wrapping up a sprint so didn't get a chance, will try soon!!