The method for configuring Serilog in Asp. Net MVC, mvcserilog
1. Introduction to Serilog
Serilog is a simple log processing method. You can use Serilog to generate a local text file or use Seq to view the specific log content on the Web interface.
Ii. configuration method
Next, we will briefly introduce how to configure Serilog to take effect in Asp. Net MVC:
1): download and install Seq. The specific download URL is [http://getseq.net/download.pdf, after installation to the terminal of the terminal, a win Service is started on the terminal, and the listening port number is 5341 by default.
The last step of installation is as follows:
Then we can find the corresponding Service in the Service list, as shown in:
2) create an Asp. Net MVC 5 project, and then download and install the corresponding package through Nuget, as shown in
3): Create a class named SerilogConfig. cs in the App_Start folder. The Code is as follows:
using Serilog;using SerilogWeb.Classic.Enrichers;using System;using System.Collections.Generic;using System.Configuration;using System.IO;using System.Linq;using System.Reflection;using System.Web;using System.Web.Hosting;namespace TestSerilog.App_Start{ public class SerilogConfig { public static ILogger CreateLogger() { var logpath = HostingEnvironment.MapPath("~"); var config = new LoggerConfiguration() .Enrich.WithMachineName() .Enrich.WithProperty("ApplicationName", AssemblyTitle) .Enrich.With<HttpRequestClientHostIPEnricher>() .Enrich.With<HttpRequestRawUrlEnricher>() .Enrich.With<HttpRequestIdEnricher>() .Enrich.With<UserNameEnricher>() //.Enrich.WithProperty("RuntimeVersion", Environment.Version) // this ensures that calls to LogContext.PushProperty will cause the logger to be enriched .Enrich.FromLogContext() .MinimumLevel.Verbose() .WriteTo.Seq(ConfigurationManager.AppSettings["SeqServer"], apiKey: ConfigurationManager.AppSettings["SeqApiKey"]) .WriteTo.RollingFile(Path.Combine(logpath, "Logs\\EricSunTestLog-{Date}.log"), retainedFileCountLimit: null, outputTemplate: "{Timestamp:yyyy-MM-dd HH:mm:ss.fff zzz} [{Level}] {SourceContext} - ({MachineName}|{HttpRequestId}|{UserName}) {Message}{NewLine}{Exception}"); return config.CreateLogger(); } public static string AssemblyTitle { get { var attributes = typeof(SerilogConfig).Assembly.GetCustomAttributes(typeof(AssemblyTitleAttribute), false); if (attributes.Length > 0) { var titleAttribute = (AssemblyTitleAttribute)attributes[0]; if (titleAttribute.Title.Length > 0) return titleAttribute.Title; } return Path.GetFileNameWithoutExtension(Assembly.GetEntryAssembly().CodeBase); } } }}
4): add the appSettings used for completing the Web. config file.
<appSettings> <add key="SeqServer" value="http://localhost:5341/" /> <add key="SeqApiKey" value="" /> </appSettings>
5): Add the following code to Startup. cs to complete registration.
using Microsoft.Owin;using Owin;using Serilog;using TestSerilog.App_Start;[assembly: OwinStartupAttribute(typeof(TestSerilog.Startup))]namespace TestSerilog{ public partial class Startup { public void Configuration(IAppBuilder app) { ConfigureAuth(app); Log.Logger = SerilogConfig.CreateLogger(); } }}
6): Add the following code to Index Action in HomeController to test the correspondingDebug
,Information
,Warning
AndError
Method
using Serilog;using System;using System.Collections.Generic;using System.Linq;using System.Web;using System.Web.Mvc;namespace TestSerilog.Controllers{ public class HomeController : Controller { private ILogger _logger = Log.Logger; public ActionResult Index() { _logger.Debug("This is index -- debug."); _logger.Information("This is index -- information."); _logger.Warning("This is index -- warning."); _logger.Error("This is index -- error."); return View(); } public ActionResult About() { ViewBag.Message = "Your application description page."; return View(); } public ActionResult Contact() { ViewBag.Message = "Your contact page."; return View(); } }}
7): directly run VS 2015 and then gohttp://localhost:5341/#/events
Observe the corresponding log records, as shown below:
Summary
In this way, the Serilog configuration is complete. You can also find the Log text file in the C: \ ProgramData \ Seq \ Logs directory. The above is all the content of this article. I hope the content of this article will help you in your study or work. If you have any questions, please leave a message.
For more information, see the following link:
Http://serilog.net/