Asp.Net MVC中配置Serilog的方法
一、Serilog介紹
Serilog 是一種非常簡(jiǎn)便記錄log 的處理方式,使用Serilog可以生成本地的text文件, 也可以通過 Seq 來在Web界面中查看具體的log內(nèi)容。
二、配置方法
接下來就簡(jiǎn)單的介紹一下在Asp.Net MVC中如何配置是Serilog 生效:
1):下載并且安裝Seq,具體的下載URL 為 【http://getseq.net/Download】,安裝到默認(rèn)的路徑之后,實(shí)際上時(shí)候啟動(dòng)了一個(gè)Win Service,并且監(jiān)聽的端口號(hào)默認(rèn)為 5341.
安裝的最后一步截圖如下:
然后我們到Service列表中可以找到對(duì)應(yīng)的Service, 如下圖所示:
2):創(chuàng)建一個(gè)Asp.Net MVC 5的一個(gè)工程, 然后通過 Nuget 下載并且安裝 對(duì)應(yīng)的 package,如下圖所示
3):在 App_Start 文件夾下創(chuàng)建一個(gè) class 叫做 SerilogConfig.cs , 代碼如下所示
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):在 Web.config 中添加補(bǔ)全所用到的 appSettings
<appSettings> <add key="SeqServer" value="http://localhost:5341/" /> <add key="SeqApiKey" value="" /> </appSettings>
5):在 Startup.cs 中添加如下代碼完成注冊(cè)
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): 在 HomeController 中的 Index Action 中添加如下代碼,測(cè)試對(duì)應(yīng)的Debug
,Information
,Warning
和 Error
方法
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):直接 VS 2015 運(yùn)行之后, 再去 http://localhost:5341/#/events
中觀察對(duì)應(yīng)的 log 記錄, 如下截圖
總結(jié)
這樣簡(jiǎn)單的配置 Serilog 就完成了, 同時(shí)我們也可以到 C:\ProgramData\Seq\Logs 目錄中找到 Log 的文本文件。以上就是本文的全部?jī)?nèi)容了,希望本文的內(nèi)容對(duì)大家的學(xué)習(xí)或者工作能帶來一定的幫助,如果有疑問大家可以留言交流。
更多內(nèi)容請(qǐng)看如下鏈接:
http://serilog.net/
相關(guān)文章
Asp.net mvc在view中用C#代碼動(dòng)態(tài)創(chuàng)建元素
這篇文章主要給大家介紹了關(guān)于Asp.net mvc如何在view中用C#代碼動(dòng)態(tài)創(chuàng)建元素的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面來一起學(xué)習(xí)學(xué)習(xí)吧2019-03-03ASP.NET Core對(duì)不同類型的用戶進(jìn)行區(qū)別限流詳解
這篇文章主要介紹了ASP.NET Core對(duì)不同類型的用戶進(jìn)行區(qū)別限流的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2021-02-02asp.net中使用cookie與md5加密實(shí)現(xiàn)記住密碼功能的實(shí)現(xiàn)代碼
雖然.net內(nèi)置了登陸控件,有記住密碼的功能,但還是想自己實(shí)踐一下,以下代碼主要應(yīng)用了COOKIE,包括安全加密的過程等2013-02-02VB.net 查詢獲取數(shù)據(jù)庫(kù)數(shù)據(jù)信息
VB.net 查詢獲取數(shù)據(jù)庫(kù)數(shù)據(jù)信息實(shí)現(xiàn)函數(shù),需要的朋友可以參考下,代碼比較簡(jiǎn)單。2009-07-07基于ABP架構(gòu)開發(fā)的.Net Core項(xiàng)目部署到IIS問題匯總
這篇文章介紹了基于ABP架構(gòu)開發(fā)的.Net Core項(xiàng)目部署到IIS問題匯總,對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2022-06-06.net平臺(tái)推送ios消息的實(shí)現(xiàn)方法
這篇文章主要介紹了.net平臺(tái)推送ios消息的實(shí)現(xiàn)方法,詳細(xì)講述了各個(gè)具體的實(shí)現(xiàn)步驟并附有源碼供大家參考之用,需要的朋友可以參考下2014-10-10ASP.NET MVC5驗(yàn)證系列之客戶端驗(yàn)證
這篇文章主要為大家詳細(xì)介紹了ASP.NET MVC5驗(yàn)證系列之客戶端驗(yàn)證,感興趣的小伙伴們可以參考一下2016-07-07ajaxToolkit:TextBoxWatermarkExtender演示與實(shí)現(xiàn)代碼
該控件的效果就是在TextBox控件上添加“水印”效果,也就是當(dāng)TextBox為空時(shí),顯示提示消息,一旦TextBox聚焦,樣式就消失,看起來還挺不錯(cuò)的嗎,感興趣的你可以了解下哦,希望本文對(duì)你有所幫助2013-01-01