When we upgraded our ASP. NET 5 sample site (about.cnblogs.com) to ASP. NET 5 RC1, we added the console logging feature.
The Add log function in ASP. Project.json is simple, just add the Microsoft.Extensions.Logging.Console reference to the:
"Dependencies": { "Microsoft.Extensions.Logging.Console": "1.0.0-*"}
and add Loggerfactory.addconsole () to Startup.cs:
using Microsoft.Extensions.Logging; namespace cnblogs.aboutus.web{ publicclass Startup { public void Configure (Iapplicationbuilder app, Iloggerfactory loggerfactory) { loggerfactory.addconsole (); }}}
You can then see the log information at the site runtime at the console:
But the console log view and find log information is inconvenient, file log is the solution. So I'm going to add the file log feature, how do I do that in ASP. NET 5?
The first thing to choose is a log component, and we start with Serilog.Framework.Logging.
Add the Serilog.Framework.Logging reference to Project.json first:
"Dependencies": { "Serilog.Framework.Logging": "1.0.0-*"}
Then run the dnu Restore command to install the NuGet package.
Next, add the following code to the Startup.cs:
usingSystem.IO;usingSerilog; Public classStartup { PublicStartup (iapplicationenvironment appenv) {Log.logger=Newloggerconfiguration (). Minimumlevel.debug (). Writeto.rollingfile (Path.Combine (Appenv.applicationbasepath,"Logs/log-{date}.txt")) . Createlogger (); } Public voidConfigure (Iapplicationbuilder app, Iloggerfactory loggerfactory) {Loggerfactor Y.addserilog (); }}
The following error occurred while running with DNX Kestrel:
dnxcore,version=v5.0 error CS1061: ' loggersinkconfiguration ' does not contain a definition for ' rollingfile ' and no Extens Ion method ' Rollingfile ' accepting a first argument of type ' loggersinkconfiguration ' could be found (is you missing a US ING directive or an assembly reference?)
and will. Writeto.rollingfile instead. Writeto.textwriter (console.out) uses the console to display log information normally.
Started thinking that a reference to a namespace was missing, and later found out because of the serilog. Rollingfile currently does not support. NET Core.
Only other log components can be found.
When you are ready to use nlog, it turns out that Nlog's file log does not support. NET Core.
Log4net don't even talk about it yet, it hasn't started supporting. NET Core.
So, have to face such a reality: currently running on Linux ASP 5 site, there is no usable log information to the file log component.
So. NET cross-platform is more than just. NET core cross-platform, and the whole. NET ecosystem, many components need to be rewritten for. NET core, which is a long way off.
. NET cross-platform journey: Increase the frustration of file log functionality