我找到了一个CodeProject,其中有一个如何使其工作的示例,但它不起作用.主要问题似乎是"Microsoft.Framework.Logging.NLog": "1.0.0-*"
Nuget中似乎不存在包.我查看了这个StackOverflow问题并查看了它引用的GitHub示例,但它似乎包含相同的问题.
我试图让它自己工作,我提出的最好的是以下内容:
public class NLogLogger : ILogger
{
public NLogLogger()
{
new WebLoggerConfigurer().ConfigureDefault();
}
public void Log(LogLevel logLevel, int eventId, object state, Exception exception, Func
BeginScopeImpl的实现似乎是一个黑客,但我不确定如何最好地实现它.
这看起来像是一个有效的实现选项吗?
有没有更好的方法来实现BeginScopeImpl?
有没有其他方法让NLog与ASP.NET 5一起使用?
Julian.. 46
Microsoft.Framework.Logging.NLog已被NLog.Extensions.Logging替换为NuGet,由NLog团队维护.
对于ASP.NET Core,您需要NLog.Web.AspNetCore(它依赖于NLog.Extensions.Logging)
如何使用:
public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory)
{
//add NLog to .NET Core
loggerFactory.AddNLog();
//Enable ASP.NET Core features (NLog.web) - only needed for ASP.NET Core users
app.AddNLogWeb();
//configure nlog.config in your project root.
env.ConfigureNLog("nlog.config");
...
//you could use LogManager.Configuration from here
public void ConfigureServices(IServiceCollection services)
{
// Add framework services.
// e.g. services.AddMvc();
//needed for NLog.Web
services.AddSingleton();
}
示例如何配置,记录和过滤,另请参阅GitHub中的README.md.
using NLog.Web;
using Microsoft.Extensions.Logging;
public static void Main(string[] args)
{
// NLog: setup the logger first to catch all errors
var logger = NLog.Web.NLogBuilder.ConfigureNLog("nlog.config").GetCurrentClassLogger();
try
{
logger.Debug("init main");
CreateWebHostBuilder(args).Build().Run();
}
catch (Exception ex)
{
//NLog: catch setup errors
logger.Error(ex, "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();
}
}
public static IWebHostBuilder CreateWebHostBuilder(string[] args) =>
WebHost.CreateDefaultBuilder(args)
.UseStartup()
.ConfigureLogging(logging =>
{
logging.ClearProviders();
logging.SetMinimumLevel(Microsoft.Extensions.Logging.LogLevel.Trace);
})
.UseNLog(); // NLog: setup NLog for Dependency injection
配置:
public class HomeController : Controller
{
public HomeController(ILogger logger)
{
logger.LogInformation("created homeController");
}
"nlog-own-2016-02-14.log"包含:
2016-02-14 16:27:04.0547 | aspnet5application.Controllers | INFO |创建homeController
2016-02-14 16:27:04.0547 | aspnet5application.Controllers | INFO |创建homeController
干杯,
朱利安(NLog)
编辑:已更新为NLog.Web.AspNetCore 4.3
Microsoft.Framework.Logging.NLog已被NLog.Extensions.Logging替换为NuGet,由NLog团队维护.
对于ASP.NET Core,您需要NLog.Web.AspNetCore(它依赖于NLog.Extensions.Logging)
如何使用:
public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory)
{
//add NLog to .NET Core
loggerFactory.AddNLog();
//Enable ASP.NET Core features (NLog.web) - only needed for ASP.NET Core users
app.AddNLogWeb();
//configure nlog.config in your project root.
env.ConfigureNLog("nlog.config");
...
//you could use LogManager.Configuration from here
public void ConfigureServices(IServiceCollection services)
{
// Add framework services.
// e.g. services.AddMvc();
//needed for NLog.Web
services.AddSingleton();
}
示例如何配置,记录和过滤,另请参阅GitHub中的README.md.
using NLog.Web;
using Microsoft.Extensions.Logging;
public static void Main(string[] args)
{
// NLog: setup the logger first to catch all errors
var logger = NLog.Web.NLogBuilder.ConfigureNLog("nlog.config").GetCurrentClassLogger();
try
{
logger.Debug("init main");
CreateWebHostBuilder(args).Build().Run();
}
catch (Exception ex)
{
//NLog: catch setup errors
logger.Error(ex, "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();
}
}
public static IWebHostBuilder CreateWebHostBuilder(string[] args) =>
WebHost.CreateDefaultBuilder(args)
.UseStartup()
.ConfigureLogging(logging =>
{
logging.ClearProviders();
logging.SetMinimumLevel(Microsoft.Extensions.Logging.LogLevel.Trace);
})
.UseNLog(); // NLog: setup NLog for Dependency injection
配置:
public class HomeController : Controller
{
public HomeController(ILogger logger)
{
logger.LogInformation("created homeController");
}
"nlog-own-2016-02-14.log"包含:
2016-02-14 16:27:04.0547 | aspnet5application.Controllers | INFO |创建homeController
2016-02-14 16:27:04.0547 | aspnet5application.Controllers | INFO |创建homeController
干杯,
朱利安(NLog)
编辑:已更新为NLog.Web.AspNetCore 4.3
更新到Julian的答案:NLog.Framework.Logging标记为过时,改为使用NLog.Extensions.Logging(只是包重命名).此外,env.ConfigureNLog("nlog.config");
仅适用于Visual Studio(IIS Express)中的调试.在测试环境中向常规IIS发布服务后,它无法启动500 internal server error
.花了很长时间才弄清楚服务nlog.config
在启动时找不到了.终于找到了NLog问题的解决方案:
public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory, IApplicationEnvironment appEnv) { ... env.ConfigureNLog(Path.Combine(appEnv.ApplicationBasePath, "nlog.config")); ... }
问题是当调试本地工作目录是你的项目目录nlog.config
内部,但在发布到IIS后,所有源文件都在%PATH_TO_PUBLISHED_SERVICE%\approot\src\%PROJECT_NAME%\
工作目录中%PATH_TO_PUBLISHED_SERVICE%\
.