1. 程式人生 > >WebAPI+NLog實現介面呼叫日誌輸出

WebAPI+NLog實現介面呼叫日誌輸出

在生產環境下,經常需要監測檢視WebAPI介面的呼叫情況,日誌就成了必不可少的手段之一,本文采用NLog實現WebAPI的日誌記錄。

環境 版本
作業系統 Windows 10 prefessional
編譯器 Visual Studio 2015 update3

建立WebAPI

建立WebAPI專案,選擇ASP.NET 4.5.2模板中的WebAPI模板。
這裡寫圖片描述

引用NLog類庫

開啟專案的NuGet包管理器,搜尋NLog,為專案新增程式包引用。
這裡寫圖片描述

修改專案配置檔案

在webAPI專案的Web.config中進行NLog的配置。首先在節點configuration>configSections下新增節點:
這裡寫圖片描述

此處name必需為nlog,否則配置資訊將不能被讀取。
然後在configuration節點下新增節點nlog:
這裡寫圖片描述

這裡定義了日誌檔案的儲存路徑、命名格式以及日誌記錄型別和監聽級別。

建立日誌跟蹤相關類

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

    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); } }

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

    public class LogFilterAttribute : 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);
        }
    }

建立異常篩選器類AbnormalFilterAttribute,繼承於System.Web.Http.Filters下的異常篩選器類,用於異常資訊的跟蹤篩選:

    public class AbnormalFilterAttribute: 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);
        }
    }

應用NLog配置

最後需要將上述配置應用到專案中去,在pp_Data>WebApiConfig中新增如下配置:
這裡寫圖片描述

將上述步驟完成後,執行專案,呼叫API介面進行測試,呼叫完成後就可以在站點目錄下找到日誌檔案,能夠為API介面的日常執行維護提供很大的便利。