Skip to content

Commit

Permalink
update with NLogBuilder
Browse files Browse the repository at this point in the history
internal logging
  • Loading branch information
304NotModified committed Oct 12, 2017
1 parent 6f861ea commit 51f8969
Show file tree
Hide file tree
Showing 5 changed files with 64 additions and 61 deletions.
6 changes: 2 additions & 4 deletions NLog.Web.AspNetCore/AspNetExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -88,10 +88,8 @@ public static IWebHostBuilder UseNLog(this IWebHostBuilder builder, NLogAspNetCo
{
ServiceLocator.ServiceProvider = serviceProvider;

//register yourself
ConfigurationItemFactory.Default.RegisterItemsFromAssembly(typeof(AspNetExtensions).Assembly);


NLogBuilder.RegisterNLogWebAspNetCore();

LogManager.Configuration?.Reload();
return new NLogLoggerFactory(options);

Expand Down
1 change: 1 addition & 0 deletions NLog.Web.AspNetCore/NLog.Web.AspNetCore.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@
<PackageReference Include="Microsoft.AspNetCore.Http.Extensions" Version="2.0.0" />
<PackageReference Include="Microsoft.AspNetCore.Hosting.Abstractions" Version="2.0.0" />
<PackageReference Include="Microsoft.AspNetCore.Routing.Abstractions" Version="2.0.0" />
<PackageReference Include="NLog" Version="4.5.0-beta04" />

</ItemGroup>
<ItemGroup>
Expand Down
60 changes: 60 additions & 0 deletions NLog.Web.AspNetCore/NLogBuilder.cs
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 &lt;extension&gt; 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
56 changes: 0 additions & 56 deletions NLog.Web.AspNetCore/NLogStart.cs

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ public class Program
public static void Main(string[] args)
{
// NLog: setup the logger first to catch all errors
var logger = NLogStart.InitLogger("NLog.config");
var logger = NLogBuilder.ConfigureNLog("NLog.config").GetCurrentClassLogger();
try
{
logger.Debug("init main");
Expand Down

0 comments on commit 51f8969

Please sign in to comment.