Created
May 30, 2019 13:34
-
-
Save aensidhe/38ac7d0e6274c10a4fce1a299917c797 to your computer and use it in GitHub Desktop.
NLog example
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
using System; | |
using System.IO; | |
using Microsoft.AspNetCore.Hosting; | |
using Microsoft.Extensions.Configuration; | |
using NLog.Web; | |
using Serilog; | |
namespace WebApplication1 | |
{ | |
public class Program | |
{ | |
public static int Main() | |
{ | |
try | |
{ | |
new WebHostBuilder() | |
.UseIISIntegration() | |
.UseKestrel() | |
.UseContentRoot(Directory.GetCurrentDirectory()) | |
.UseWebRoot(Path.Combine(Directory.GetCurrentDirectory(), "wwwroot")) | |
.ConfigureAppConfiguration((x, builder) => builder | |
.SetBasePath(x.HostingEnvironment.ContentRootPath) | |
.AddYamlFile("appsettings.yaml", false, true)) | |
.UseStartup<Startup>() | |
.UseDefaultServiceProvider((context, options) => options.ValidateScopes = context.HostingEnvironment.IsDevelopment()) | |
.UseSerilog((context, configuration) => configuration.ReadFrom.Configuration(context.Configuration)) | |
.UseNLog() | |
.Build() | |
.Run(); | |
return 0; | |
} | |
catch (Exception ex) | |
{ | |
Log.Fatal(ex, "Host terminated unexpectedly"); | |
Console.WriteLine(ex); | |
return 1; | |
} | |
finally | |
{ | |
Log.CloseAndFlush(); | |
} | |
} | |
} | |
} |
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
using Microsoft.AspNetCore.Builder; | |
using Microsoft.AspNetCore.Hosting; | |
using Microsoft.AspNetCore.Mvc; | |
using Microsoft.Extensions.Configuration; | |
using Microsoft.Extensions.DependencyInjection; | |
using Microsoft.Extensions.Logging; | |
namespace WebApplication1 | |
{ | |
public class Startup | |
{ | |
public Startup(IConfiguration configuration) | |
{ | |
Configuration = configuration; | |
} | |
public IConfiguration Configuration { get; } | |
// This method gets called by the runtime. Use this method to add services to the container. | |
public void ConfigureServices(IServiceCollection services) | |
{ | |
services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_2); | |
} | |
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline. | |
public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory factory) | |
{ | |
if (env.IsDevelopment()) | |
{ | |
app.UseDeveloperExceptionPage(); | |
} | |
else | |
{ | |
// The default HSTS value is 30 days. You may want to change this for production scenarios, see https://aka.ms/aspnetcore-hsts. | |
app.UseHsts(); | |
} | |
app.UseHttpsRedirection(); | |
app.UseMvc(); | |
factory.CreateLogger<Startup>().LogCritical("asd"); | |
} | |
} | |
} |
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
<Project Sdk="Microsoft.NET.Sdk.Web"> | |
<PropertyGroup> | |
<TargetFrameworks>netcoreapp2.2</TargetFrameworks> | |
<TargetLatestRuntimePatch>true</TargetLatestRuntimePatch> | |
<LangVersion>latest</LangVersion> | |
<TreatWarningsAsErrors>true</TreatWarningsAsErrors> | |
<WarningLevel>4</WarningLevel> | |
</PropertyGroup> | |
<ItemGroup> | |
<PackageReference Include="Microsoft.AspNetCore" Version="2.2.0" /> | |
<PackageReference Include="Microsoft.AspNetCore.HttpsPolicy" Version="2.2.0" /> | |
<PackageReference Include="Microsoft.AspNetCore.Mvc" Version="2.2.0" /> | |
<PackageReference Include="NetEscapades.Configuration.Yaml" Version="1.6.0" /> | |
<PackageReference Include="Serilog" Version="2.8.0" /> | |
<PackageReference Include="Serilog.AspNetCore" Version="2.1.1" /> | |
<PackageReference Include="Serilog.Enrichers.Environment" Version="2.1.2" /> | |
<PackageReference Include="Serilog.Settings.Configuration" Version="3.0.1" /> | |
<PackageReference Include="Serilog.Sinks.RollingFile" Version="3.3.0" /> | |
<PackageReference Include="NLog.Web.AspNetCore" Version="4.8.0" /> | |
<PackageReference Include="NLog" Version="4.5.11" /> | |
</ItemGroup> | |
<ItemGroup> | |
<Content Update="nlog.config" CopyToOutputDirectory="PreserveNewest" /> | |
</ItemGroup> | |
</Project> |
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
Serilog: | |
Using: | |
- Serilog.Sinks.RollingFile | |
WriteTo: | |
- Name: RollingFile | |
Args: | |
pathFormat: 234.txt |
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
<?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="internal-nlog.txt"> | |
<!-- enable asp.net core layout renderers --> | |
<extensions> | |
<add assembly="NLog.Web.AspNetCore"/> | |
</extensions> | |
<!-- the targets to write to --> | |
<targets> | |
<!-- write logs to file --> | |
<target xsi:type="File" name="allfile" fileName="nlog-all-${shortdate}.log" | |
layout="${longdate}|${event-properties:item=EventId_Id}|${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="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}" /> | |
</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="*" minlevel="Trace" writeTo="ownFile-web" /> | |
</rules> | |
</nlog> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment