1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 |
public void Configure(IApplicationBuilder app, IWebHostEnvironment env) { // 添加错误处理 app.UseExceptionHandler(a => a.Run(async context => { var feature = context.Features.Get<Microsoft.AspNetCore.Diagnostics.IExceptionHandlerPathFeature>(); var ext = feature.Error; DCSoft.DCMiddleWareForWebServerLog.LogError(context, ext);// 记录错误 byte[] bs = System.Text.Encoding.UTF8.GetBytes("<pre>" + ext.ToString() + "</pre>"); context.Response.StatusCode = 500; context.Response.ContentType = "text/html;charset=utf-8"; await context.Response.Body.WriteAsync(bs, 0, bs.Length); })); app.UseHsts(); app.UseHttpsRedirection(); app.UseStaticFiles(); app.UseCookiePolicy(); app.UseSession(); app.UseRouting(); app.UseAuthorization(); // 添加操作中间件 app.UseMiddleware<DCSoft.DCMiddleWareForWebServerLog>(System.IO.Path.Combine(env.WebRootPath, "LogFiles")); app.UseEndpoints(endpoints => { endpoints.MapControllerRoute( name: "default", pattern: "{controller=DCWriterDemo}/{action=Index}/{id?}"); }); } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 |
public class Global : System.Web.HttpApplication { protected void Application_Start(object sender, EventArgs e) { // 设置数据文件保存路径,将在该目录下创建类似dcsoft_serverlog_20200601.dat数据文件 // 每天一个文件。这个文件可以用于在线分析,也可以复制到他处进行离线分析。 DCSoft.DCWebServerLog.StartSpecifyWorkDirectory(this.Server.MapPath(".")); } protected void Application_BeginRequest(object sender, EventArgs e) { // 开始记录一次请求 DCSoft.DCWebServerLog.BeginRequest( this.Request, this.Request.RawUrl, this.Request.ContentLength); } protected void Application_EndRequest(object sender, EventArgs e) { // 结束一次记录 DCSoft.DCWebServerLog.EndRequest(this.Request, 0); } protected void Application_Error(object sender, EventArgs e) { // 记录一次错误信息 DCSoft.DCWebServerLog.SetErrorMessage(this.Request, this.Server.GetLastError()); } protected void Session_Start(object sender, EventArgs e) { // 开始创建一个客户端连接信息,累计ASP.NET并发数 DCSoft.DCWebServerLog.SessionStart( this.Session.SessionID, this.Session.Timeout); } protected void Session_End(object sender, EventArgs e) { // 结束一个客户端连接信息,更新ASP.NET并发数。 DCSoft.DCWebServerLog.SessionEnd(this.Session.SessionID); } } |
<configuration> <appSettings> <add key="DCWriterServerLogFileDirectory" value="c:\wwwroot\dcwriter-log"/> </appSettings> </configuration>