webapi日誌記錄(TXT存儲)

来源:https://www.cnblogs.com/my2020/archive/2020/03/10/12457872.html
-Advertisement-
Play Games

記錄webapi日誌我使用了兩種辦法,一種是存儲TXT的log文檔,但我發現使用使用了我介面的日誌都會存儲到我電腦上。後面改用資料庫存儲log。資料庫存儲log信息這種方法個人比較推薦。之前花費了一些時間來寫TXT存儲還是想記錄下來。 轉載自:https://blog.csdn.net/lordwi ...


記錄webapi日誌我使用了兩種辦法,一種是存儲TXT的log文檔,但我發現使用使用了我介面的日誌都會存儲到我電腦上。後面改用資料庫存儲log。資料庫存儲log信息這種方法個人比較推薦。之前花費了一些時間來寫TXT存儲還是想記錄下來。

 

轉載自:https://blog.csdn.net/lordwish/article/details/72353851

1、引用NLog類庫

打開項目的NuGet包管理器,搜索NLog,為項目添加程式包引用。

 

2、修改項目配置文件

在webAPI項目的Web.config中進行NLog的配置。首先在節點configuration>configSections下添加節點:

此處name必需為nlog,否則配置信息將不能被讀取。 然後在configuration節點下添加節點nlog:

這裡定義了日誌文件的保存路徑、命名格式以及日誌記錄類型和監聽級別。

 註意:<configSections>必須要緊跟在<configuration>下方

<configuration>
   <configSections>
      <section name="nlog" type="NLog.Config.ConfigSectionHandler,NLog" />
    </configSections>

  <nlog xmlns:xsi="http://www.w3.org/2001/XMLSchema">
    <targets>
      <target name="logfile" xsi:type="File" fileName="${basedir}/LogFile/${date:format=yyyy/MM/dd}-api.txt"/>
      <target name="eventlog" xsi:type="EventLog" layout="${message}" log="Application" source="Api Services"/>
    </targets>
    <rules>
      <logger name="*" minlevel="Trace" writeTo="logfile"/>
      <logger name="*" minlevel="Trace" writeTo="eventlog"/>
    </rules>    
  </nlog>
</configuration>
View Code

 3、創建日誌及跟蹤類

創建日誌跟蹤類AppLog,繼承於System.Web.Http.Tracing下的跟蹤編寫器介面ITraceWriter,用於日誌生成和寫入:

using Newtonsoft.Json;
using NLog;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net.Http;
using System.Text;
using System.Web;
using System.Web.Http.Tracing;

namespace InsideMesAPI.Log
{
    public sealed class AppLog : ITraceWriter
    {
        //日誌寫入
        private static readonly Logger AppLogger = LogManager.GetCurrentClassLogger();
        private static readonly Lazy<Dictionary<TraceLevel, Action<string>>> LoggingMap = new Lazy<Dictionary<TraceLevel, Action<string>>>(() => new Dictionary<TraceLevel, Action<string>>
        {
            {TraceLevel.Info,AppLogger.Info },
            {TraceLevel.Debug,AppLogger.Debug },
            {TraceLevel.Error,AppLogger.Error },
            {TraceLevel.Fatal,AppLogger.Fatal },
            {TraceLevel.Warn,AppLogger.Warn }
        });


        private Dictionary<TraceLevel, Action<string>> Logger
        {
            get { return LoggingMap.Value; }
        }
        /// <summary>
        /// 跟蹤編寫器介面實現
        /// </summary>
        /// <param name="request"></param>
        /// <param name="category"></param>
        /// <param name="level"></param>
        /// <param name="traceAction"></param>
        public void Trace(HttpRequestMessage request, string category, TraceLevel level, Action<TraceRecord> traceAction)
        {
            if (level != TraceLevel.Off)//未禁用日誌跟蹤
            {
                if (traceAction != null && traceAction.Target != null)
                {
                    category = category + Environment.NewLine + "Action Parameters : " + JsonConvert.SerializeObject(traceAction.Target);
                }
                var record = new TraceRecord(request, category, level);
                if (traceAction != null)
                {
                    traceAction(record);
                }
                //  traceAction?.Invoke(record);
                Log(record);
            }
            //throw new NotImplementedException();
        }
        /// <summary>
        /// 日誌寫入
        /// </summary>
        /// <param name="record"></param>
        private void Log(TraceRecord record)
        {
            var message = new StringBuilder();

            /**************************運行日誌****************************/

            if (!string.IsNullOrWhiteSpace(record.Message))
            {
                message.Append("").Append(record.Message + Environment.NewLine);
            }

            if (record.Request != null)
            {
                if (record.Request.Method != null)
                {
                    message.Append("Method : " + record.Request.Method + Environment.NewLine);
                }

                if (record.Request.RequestUri != null)
                {
                    message.Append("").Append("URL : " + record.Request.RequestUri + Environment.NewLine);
                }

                if (record.Request.Headers != null && record.Request.Headers.Contains("Token") && record.Request.Headers.GetValues("Token") != null && record.Request.Headers.GetValues("Token").FirstOrDefault() != null)
                {
                    message.Append("").Append("Token : " + record.Request.Headers.GetValues("Token").FirstOrDefault() + Environment.NewLine);
                }
            }

            if (!string.IsNullOrWhiteSpace(record.Category))
            {
                message.Append("").Append(record.Category);
            }

            //if (!string.IsNullOrWhiteSpace(record.Operator))
            //{
            //    message.Append(" ").Append(record.Operator).Append(" ").Append(record.Operation);
            //}

            //***************************異常日誌***********************************//
            if (record.Exception != null && !string.IsNullOrWhiteSpace(record.Exception.GetBaseException().Message))
            {
                var exceptionType = record.Exception.GetType();
                message.Append(Environment.NewLine);
                message.Append("").Append("Error : " + record.Exception.GetBaseException().Message + Environment.NewLine);

            }


            //日誌寫入本地文件
            Logger[record.Level](Convert.ToString(message) + Environment.NewLine);
        }
    }
}
View Code

 創建日誌篩選器類LogFilterAttribute,繼承於System.Web.Http.Filters下的篩選器特性基類,用於定義日誌內容:

using System;
using System.Web.Http;
using System.Web.Http.Controllers;
using System.Web.Http.Tracing;

namespace InsideMesAPI.Log
{
    public class LogFilterAttribute : System.Web.Http.Filters.ActionFilterAttribute
    {
        public override void OnActionExecuting(HttpActionContext actionContext)
        {
            GlobalConfiguration.Configuration.Services.Replace(typeof(ITraceWriter), new AppLog());
            var trace = GlobalConfiguration.Configuration.Services.GetTraceWriter();
            //trace.Info(actionContext.Request, "Controller : " + actionContext.ControllerContext.ControllerDescriptor.ControllerType.FullName + Environment.NewLine + "Action : " + actionContext.ActionDescriptor.ActionName, "JSON", actionContext.ActionArguments);
            //base.OnActionExecuting(actionContext);
        }
    }
}
View Code

創建異常篩選器類AbnormalFilterAttribute,繼承於System.Web.Http.Filters下的異常篩選器類,用於異常信息的跟蹤篩選:

using System;
using System.ComponentModel.DataAnnotations;
using System.Net;
using System.Net.Http;
using System.Web.Http;
using System.Web.Http.Filters;
using System.Web.Http.Tracing;


namespace InsideMesAPI.Log
{
    public class AbnormalFilterAttribute : System.Web.Http.Filters.ExceptionFilterAttribute
    {
        public override void OnException(HttpActionExecutedContext actionExecutedContext)
        {
            GlobalConfiguration.Configuration.Services.Replace(typeof(ITraceWriter), new AppLog());
            var trace = GlobalConfiguration.Configuration.Services.GetTraceWriter();
            trace.Error(actionExecutedContext.Request, "Controller : " + actionExecutedContext.ActionContext.ControllerContext.ControllerDescriptor.ControllerType.FullName + Environment.NewLine + "Action : " + actionExecutedContext.ActionContext.ActionDescriptor.ActionName, actionExecutedContext.Exception);
            var exceptionType = actionExecutedContext.Exception.GetType();
            if (exceptionType == typeof(ValidationException))
            {
                var resp = new HttpResponseMessage(HttpStatusCode.BadRequest) { Content = new StringContent(actionExecutedContext.Exception.Message), ReasonPhrase = "ValidationException" };
                throw new HttpResponseException(resp);
            }
            else if (exceptionType == typeof(UnauthorizedAccessException))
            {
                throw new HttpResponseException(actionExecutedContext.Request.CreateResponse(HttpStatusCode.Unauthorized));
            }
            else
            {
                throw new HttpResponseException(actionExecutedContext.Request.CreateResponse(HttpStatusCode.InternalServerError));
            }
            //base.OnException(actionExecutedContext);
        }
    }
}
View Code

 

4、應用配置

 public static class WebApiConfig
    {
        public static void Register(HttpConfiguration config)
        {
            日誌配置
            config.Filters.Add(new Log.LogFilterAttribute());
            config.Filters.Add(new Log.AbnormalFilterAttribute());
         }
     }
View Code

 


您的分享是我們最大的動力!

-Advertisement-
Play Games
更多相關文章
  • 結構體 用一組變數定義一個事物 struct student //student 是一種數據類型 { int id; char name[20]; char sex; int age; }mike,bob;變數定義除了跟在結構體定義後面,還可以:student std1,std2;跟int a,b; ...
  • uwsgi uWSGI是一個Web伺服器,它實現了WSGI協議、uwsgi、http等協議。Nginx中HttpUwsgiModule的作用是與uWSGI伺服器進行交換。 WSGI是一種Web伺服器網關介面。它是一個Web伺服器(如nginx,uWSGI等伺服器)與web應用(如用Flask框架寫的 ...
  • springboot攔截器 說明:和springmvc一樣去實現HandlerInterceptor介面 import org.springframework.stereotype.Component; import org.springframework.web.servlet.HandlerIn ...
  • 準備工作: 使用環境 :PHPStudy 開啟Apache和Mysql 打開代碼編輯器 <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Calculator</title> </head> <body> <f ...
  • 1. 方法 2. 例子 (1)增加/修改 D[key] = value 1 >>> D = {1: 'a', 2: 'b', 3: 'c'} 2 >>> D[4] = 'd' 3 >>> D[1] = 'A' 4 >>> D 5 {1: 'A', 2: 'b', 3: 'c', 4: 'd'} 6 ...
  • 本文主要介紹const修飾符在C++中的主要用法,下麵會從兩個方面進行介紹: 類定義中使用const 、 非類定義中使用const 1. 非類定義中使用const 非類定義中使用const是指:在除了類定義以外的場景中使用const。 1.1 變數 頂層const:變數本身是個常量 底層const: ...
  • 我們見慣了上古時代,由電子管組成的時鐘。在 AI 時代,有沒有更加新潮的時間顯示方式?是的,數據集也能做成時鐘,每天早上,讓 MNIST 手寫數字喚醒你一天的記憶。 ​ MNIST 是電腦視覺中不老的經典,當第一代捲積神經網路在這個手寫數字數據集上綻放出耀眼的光芒,它註定會載入「史冊」。 儘管目前 ...
  • 註冊中心zookeeper 什麼是註冊中心: 註冊中心就是用來存儲服務信息的地方,就像房屋中介一樣; 為什麼需要註冊中心: 在前面的例子中我們使用了客戶端與伺服器直連的方式完成了服務的調用,在實際開發中這回帶來一些問題,例如伺服器地址變更了,或服務搭建了集群,客戶端不知道服務的地址,此時註冊中心就派 ...
一周排行
    -Advertisement-
    Play Games
  • 移動開發(一):使用.NET MAUI開發第一個安卓APP 對於工作多年的C#程式員來說,近來想嘗試開發一款安卓APP,考慮了很久最終選擇使用.NET MAUI這個微軟官方的框架來嘗試體驗開發安卓APP,畢竟是使用Visual Studio開發工具,使用起來也比較的順手,結合微軟官方的教程進行了安卓 ...
  • 前言 QuestPDF 是一個開源 .NET 庫,用於生成 PDF 文檔。使用了C# Fluent API方式可簡化開發、減少錯誤並提高工作效率。利用它可以輕鬆生成 PDF 報告、發票、導出文件等。 項目介紹 QuestPDF 是一個革命性的開源 .NET 庫,它徹底改變了我們生成 PDF 文檔的方 ...
  • 項目地址 項目後端地址: https://github.com/ZyPLJ/ZYTteeHole 項目前端頁面地址: ZyPLJ/TreeHoleVue (github.com) https://github.com/ZyPLJ/TreeHoleVue 目前項目測試訪問地址: http://tree ...
  • 話不多說,直接開乾 一.下載 1.官方鏈接下載: https://www.microsoft.com/zh-cn/sql-server/sql-server-downloads 2.在下載目錄中找到下麵這個小的安裝包 SQL2022-SSEI-Dev.exe,運行開始下載SQL server; 二. ...
  • 前言 隨著物聯網(IoT)技術的迅猛發展,MQTT(消息隊列遙測傳輸)協議憑藉其輕量級和高效性,已成為眾多物聯網應用的首選通信標準。 MQTTnet 作為一個高性能的 .NET 開源庫,為 .NET 平臺上的 MQTT 客戶端與伺服器開發提供了強大的支持。 本文將全面介紹 MQTTnet 的核心功能 ...
  • Serilog支持多種接收器用於日誌存儲,增強器用於添加屬性,LogContext管理動態屬性,支持多種輸出格式包括純文本、JSON及ExpressionTemplate。還提供了自定義格式化選項,適用於不同需求。 ...
  • 目錄簡介獲取 HTML 文檔解析 HTML 文檔測試參考文章 簡介 動態內容網站使用 JavaScript 腳本動態檢索和渲染數據,爬取信息時需要模擬瀏覽器行為,否則獲取到的源碼基本是空的。 本文使用的爬取步驟如下: 使用 Selenium 獲取渲染後的 HTML 文檔 使用 HtmlAgility ...
  • 1.前言 什麼是熱更新 游戲或者軟體更新時,無需重新下載客戶端進行安裝,而是在應用程式啟動的情況下,在內部進行資源或者代碼更新 Unity目前常用熱更新解決方案 HybridCLR,Xlua,ILRuntime等 Unity目前常用資源管理解決方案 AssetBundles,Addressable, ...
  • 本文章主要是在C# ASP.NET Core Web API框架實現向手機發送驗證碼簡訊功能。這裡我選擇是一個互億無線簡訊驗證碼平臺,其實像阿裡雲,騰訊雲上面也可以。 首先我們先去 互億無線 https://www.ihuyi.com/api/sms.html 去註冊一個賬號 註冊完成賬號後,它會送 ...
  • 通過以下方式可以高效,並保證數據同步的可靠性 1.API設計 使用RESTful設計,確保API端點明確,並使用適當的HTTP方法(如POST用於創建,PUT用於更新)。 設計清晰的請求和響應模型,以確保客戶端能夠理解預期格式。 2.數據驗證 在伺服器端進行嚴格的數據驗證,確保接收到的數據符合預期格 ...