WebAPI+NLog实现接口调用日志输出
Posted 那一缕阳光
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了WebAPI+NLog实现接口调用日志输出相关的知识,希望对你有一定的参考价值。
在生产环境下,经常需要监测查看WebAPI接口的调用情况,日志就成了必不可少的手段之一,本文采用NLog实现WebAPI的日志记录。
环境 版本
操作系统 Windows 10 prefessional
编译器 Visual Studio 2015
创建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接口的日常运行维护提供很大的便利。
以上是关于WebAPI+NLog实现接口调用日志输出的主要内容,如果未能解决你的问题,请参考以下文章
新技能:NLog输出结构化日志,并在Kibana优雅分析日志?