使用Exceptionless记录日志
第一部分:搭建本地Exceptionless環境
?
提前先下載好對應的程序包
- .NET 4.6.1。如果已安裝過VS請忽略。
- JDK??
- elasticsearch
- Exceptionless
?
第一步,安裝JDK并配置環境變量。
第二步,解壓elasticsearch,進入bin目錄,運行elasticsearch.bat。
?
第三步,解壓 Exceptionless,? 運行Start.bat。
?
?在執行Start.bat的時候,如果出現以下錯誤,請參考PowerShell設置。
?
如果沒有報錯,會出現這個界面。
等待安裝完成,會自動打開http://localhost:50000/這個頁面。
在低版本的安裝過程中,需要修改各種配置文件,在最新版本已經不需要手動去修改了,完全是傻瓜式安裝。
到此為止,安裝流程已經全部完成了。
?
?
第二部分:代碼調用
?在登錄頁面創建好賬號并登錄,新建一個項目。
?
點擊“Add Project”后,會引導選擇使用的框架,然后會給出對應框架的使用語句。
?
在VS中創建一個新的測試項目,我這邊的代碼和平臺給的示例稍微有點不同,我把配置寫在了json文件中。
?
Startup類里面加上這兩行代碼,把服務注冊到系統中。
?
最后在控制器里面調用對應的方法。
1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Threading.Tasks; 5 using Exceptionless; 6 using Exceptionless.Logging; 7 using Microsoft.AspNetCore.Mvc; 8 9 namespace ExceptionlessLogTest_V2.Controllers 10 { 11 [Route("api/[controller]")] 12 public class ValuesController : Controller 13 { 14 // GET api/values 15 [HttpGet] 16 public string Get() 17 { 18 ELogInfo("Get", $"logging-date:{DateTime.Now}"); 19 try 20 { 21 throw new Exception("Unable to create order from quote."); 22 } 23 catch (Exception ex) 24 { 25 ELogError(ex, "err"); 26 } 27 28 return "執行完成"; 29 } 30 31 32 public void ELogInfo(string source, string message, string tag = "") 33 { 34 if (string.IsNullOrEmpty(tag)) 35 { 36 ExceptionlessClient.Default.CreateLog(source, message, LogLevel.Info).Submit(); 37 } 38 else 39 { 40 ExceptionlessClient.Default.CreateLog(source, message, LogLevel.Info).AddTags(tag).Submit(); 41 } 42 } 43 44 45 public void ELogError(Exception ex, string tag = "") 46 { 47 if (string.IsNullOrEmpty(tag)) 48 { 49 ExceptionlessClient.Default.SubmitException(ex); 50 } 51 else 52 { 53 ExceptionlessClient.Default.CreateException(ex).SetProperty("ex", tag).Submit(); 54 } 55 } 56 57 } 58 } View Code?
更多的使用,請參考官方文檔或者?savorboard的文章?
?
在調用了Get這個接口之后,對應的日志信息和異常信息已經記錄到Exceptionless中了。最終效果如下:
??
?
參考鏈接:
http://www.cnblogs.com/liying0721/p/7027121.html#3738569
https://www.cnblogs.com/savorboard/p/exceptionless.html
轉載于:https://www.cnblogs.com/jiao006/p/7652431.html
總結
以上是生活随笔為你收集整理的使用Exceptionless记录日志的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 如何实现分类表统计数目和详情表数量同步
- 下一篇: HTML哪些是块级元素,哪些是行内元素、