-
Notifications
You must be signed in to change notification settings - Fork 73
NLog
ZhangYang edited this page Jul 7, 2019
·
3 revisions
Nlog was used in RestAiline for logging ASP.NET Core logging
- NLog.Web.AspNetCore 4.8+
- Update the NLog package if possible
in csproj:
<PackageReference Include="NLog.Web.AspNetCore" Version="4.8.0" />
<PackageReference Include="NLog" Version="4.5.11" />
Create nlog.config file in root of the api project:
Edit csproj:
<ItemGroup>
<Content Update="nlog.config">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</Content>
</ItemGroup>
Inject ILogger into your class:
public class HomeController : Controller
{
private readonly ILogger<HomeController> _logger;
public HomeController(ILogger<HomeController> logger)
{
_logger = logger;
}
public IActionResult Index()
{
_logger.LogInformation("Index page says hello");
return View();
}
}
Checkin console and Logs folder in bin
Write a filte for intercept unhandled exceptions:
public class UnhandledExceptionFilter : IExceptionFilter
{
private readonly ILogger<UnhandledExceptionFilter> _logger;
public UnhandledExceptionFilter(ILogger<UnhandledExceptionFilter> logger)
{
_logger = logger;
}
public void OnException(ExceptionContext context)
{
_logger.LogCritical(context.Exception, "Unhandled exception");
}
}
Add this filter in MVC configuration:
services.AddMvc(options =>
{
options.Filters.Add<UnhandledExceptionFilter>();
});
-
Getting started
-
ASP.NET Core
-
Docker & Docker-compose
-
Hypermedia API
-
EntityFramework ReadModel
-
MongoDb ReadModel
-
Elasticseach ReadModel
-
DDD
-
Domain