In the previous blog post (the frustration of adding file log functionality), we ran into a problem: though there were some. NET-log components (such as Serilog, NLog) have started to support. NET Core, but currently only support console output logs, which do not support writing the log to files; This means that the example of our running on Linux ASP. 5 site cannot write the log to a file. It's a lot of trouble to troubleshoot problems, such as the example sites that are often hung up.
Faced with this problem we did not let go, do not want to be affected by this problem. NET cross-platform journey of the pace, we want to solve it, and want to first solve with a simple method, do not want to implement a log component from scratch.
After the last post, a colleague gave the hint that since Serilog logs the log information directly to the console's output stream ( . Writeto.textwriter (console.out) ), then it should also write to the file's write stream, which is the stream.
Good idea! Feasible! But there is a premise that the file Operation class Library in Corefx to support Linux, that is, System.IO.FileSystem implementation of the cross-platform.
Is the file Operations Class library in. NET Core already cross-platform? Can our file log problem be solved by it? Code, you will know.
So the Startup.cs will be in the. Writeto.textwriter (console.out) change to the following code:
. Writeto.textwriter (new StreamWriter (LogFilePath))
The following error occurred while running the DNX Kestrel command:
dnxcore,version=v5.0 error cs1503:argument 1:cannot convert from ' string ' to ' System.IO.Stream '
This error occurs because the StreamWriter constructor implemented in Corefx does not support file paths as parameters and is supported in the. NET framework. So you need to modify the code to pass the parameters of the FileStream type to the StreamWriter constructor, the code is as follows:
. Writeto.textwriter (new StreamWriter (new FileStream (LogFilePath, FileMode.OpenOrCreate)))
After this change, the site starts successfully.
Logging to/data/git/aboutus/logs/log.txthosting Environment:productionnow Listening on:http://*:8001application Started. Press CTRL + C to shut down.
Then use the browser to access the site, and use the tail command to see if the log was successfully written to the log file?
# tail log.txt11/22/2015 14:08:58 +08:00 [Debug] The View ' "Intro" ' was found.11/22/2015 14:08:58 +08:00 [information] Exe Cuting ViewResult, running view at path '/views/about/intro.cshtml '. 11/22/2015 14:08:58 +08:00 [information] User profile is available. Using ' "/root/.aspnet/dataprotection-keys" ' as key repository; Keys is not being encrypted at rest.11/22/2015 14:09:01 +08:00 [Debug] Data source=xxx11/22/2015 14:09:03 +08:00 [Informati On] microsoft.data.entity.storage.dbcommandlogdata11/22/2015 14:09:03 +08:00 [Debug] Data source=xxx11/22/2015 14:09:03 +08:00 [information] Executed action "CNBlogs.AboutUs.Web.AboutController.Intro" in 0.6192ms11/22/2015 14:09:03 +08:00 [Information] Request finished in 0.6196ms text/html; charset=utf-811/22/2015 14:09:16 +08:00 [ERROR] Connection ID "4" disconnected.11/22/2015 14:09:16 +08:00 [ERROR] Connect Ion ID "3" disconnected.
Success! The problem of logging the log-to-file of an ASP. NET 5 site on Linux is temporarily resolved.
By the way, share the latest code in the current Startup.cs:
usingSystem;usingSystem.IO;usingSystem.Linq;usingMicrosoft.AspNet.Builder;usingMicrosoft.Extensions.DependencyInjection;usingMicrosoft.Data.Entity;usingCNBlogs.AboutUs.Data;usingMicrosoft.Dnx.Runtime;usingMicrosoft.Extensions.PlatformAbstractions;usingMicrosoft.Extensions.Configuration;usingSystem.Data.SqlClient;usingMicrosoft.Extensions.Logging;usingSerilog;namespacecnblogs.aboutus.web{ Public classStartup { PublicStartup (iapplicationenvironment appenv) {Iconfigurationbuilder builder=NewConfigurationbuilder (). Setbasepath (Appenv.applicationbasepath). Addjsonfile ("Config.json",false); Configuration=Builder. Build (); varLogFilePath = Path.Combine (Appenv.applicationbasepath,"Logs/log.txt"); Console.WriteLine ("Logging to"+LogFilePath); Log.logger=Newloggerconfiguration (). Minimumlevel.debug (). Writeto.textwriter (NewStreamWriter (NewFileStream (LogFilePath, FileMode.OpenOrCreate))) . Createlogger (); } PublicIConfiguration Configuration {Get;Set; } Public voidConfigure (Iapplicationbuilder app, Iloggerfactory loggerfactory) {loggerfactory. Addserilog (). Addconsole (); App. Usedeveloperexceptionpage (); App. Usemvcwithdefaultroute (); App. Usestaticfiles (); App. Useruntimeinfopage (); } Public voidconfigureservices (iservicecollection services) {services. Addmvc (); Services. Addentityframework (). Addsqlserver (). Adddbcontext<EfDbContext> (options ={options. Usesqlserver (configuration["data:connectionstring"]); }); Services. AddTransient<itabnavrepository, tabnavrepository>(); } }}
. NET Cross-platform tour: Write an ASP. Log to a file on Linux