.net core 进入每个页面都添加日志
添加中间件判断读取资源是否是页面,如果是页面则添加日志:
public void Configure(IApplicationBuilder app, IHostingEnvironment env) { app.Use(c => { return new RequestDelegate( async context => { // await context.Response.WriteAsync("Hello world start"); //使用Contains则RequestDelegate会报错所以改用IndexOf if (context.Request.Path.Value.IndexOf(".html") >= 0) { string ticket = context.Request.Query["ticket"].ToString(); Guid userGuid; string s0; DateTime dt;
//如果ticket有效,能够正常找到用户名,则添加日志, if (TicketRingContext.Parse(ticket, out userGuid, out s0, out dt)) { LogInfo LOG = new LogInfo(); LOG.Title = "进入页面"; LOG.Content = context.Request.Path.Value; LOG.OperationAction = "140000"; LOG.MenuId = "301"; LOG.ticket = ticket; LOG.SessionId = ""; LOG.System = EnumSysNo.个人工作台.ToString(); LOG.Remark = ""; LogRedis.WriteLogRedis(LOG); } } await c.Invoke(context); } ); }); }