C# WebService接口耗時監控,執行超過1秒鐘做日誌記錄

C# WebService接口耗時監控,執行超過1秒鐘做日誌記錄

在做WebService接口耗時監控之前,需要對HttpApplication類的理解。他將是整個ASP.NET生命週期的管理,是ASP.NET的基礎。

詳情參考https://docs.microsoft.com/zh-cn/dotnet/api/system.web.httpapplication?view=netframework-4.8。

瞭解ASP.NET中HttpApplication中事件的過程順序。

1、Application_BeginRequest
2、Application_AuthenticateRequest
3、Application_PostAuthenticateRequest
4、Application_AuthorizeRequest
5、Application_PostAuthorizeRequest
6、Application_ResolveRequestCache
7、Application_AcquireRequestState
8、Application_PostUpdateRequestCache
9、Application_PreRequestHandlerExecute
執行自定義方法。。。
10、Application_PostRequestHandlerExecute
11、Application_ReleaseRequestState
12、Application_PostReleaseRequestState
13、Application_UpdateRequestCache
14、Application_PostUpdateRequestCache
15、Application_LogRequest
16、Application_PostLogRequest
17、Application_EndRequest
18、Application_PreSendRequestHeaders
19、Application_PreSendRequestContent
C# WebService接口耗時監控,執行超過1秒鐘做日誌記錄


1、新建一個Global.asax文件測試上述過程。 Global類繼承HttpApplication類,完整測試代碼

using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.SessionState;
namespace MyService
{
public class Global : System.Web.HttpApplication
{
private int index = 0;
protected void Application_Start(object sender, EventArgs e)
{
}
public void Init(HttpApplication application)
{
application.BeginRequest += (new EventHandler(this.Application_BeginRequest));
application.AuthenticateRequest += (new EventHandler(this.Application_AuthenticateRequest));
application.PostAuthenticateRequest += (new EventHandler(this.Application_PostAuthenticateRequest));
application.AuthorizeRequest += (new EventHandler(this.Application_AuthorizeRequest));
application.PostAuthorizeRequest += (new EventHandler(this.Application_PostAuthorizeRequest));
application.ResolveRequestCache += (new EventHandler(this.Application_ResolveRequestCache));
application.AcquireRequestState += (new EventHandler(this.Application_AcquireRequestState));
application.PostUpdateRequestCache += (new EventHandler(this.Application_PostUpdateRequestCache));
application.PreRequestHandlerExecute += (new EventHandler(this.Application_PreRequestHandlerExecute));
application.PostRequestHandlerExecute += (new EventHandler(this.Application_PostRequestHandlerExecute));
application.ReleaseRequestState += (new EventHandler(this.Application_ReleaseRequestState));
application.PostReleaseRequestState += (new EventHandler(this.Application_PostReleaseRequestState));
application.UpdateRequestCache += (new EventHandler(this.Application_UpdateRequestCache));
application.PostUpdateRequestCache += (new EventHandler(this.Application_PostUpdateRequestCache));
application.LogRequest += (new EventHandler(this.Application_LogRequest));
application.PostLogRequest += (new EventHandler(this.Application_PostLogRequest));
application.EndRequest += (new EventHandler(this.Application_EndRequest));
application.PreSendRequestHeaders += (new EventHandler(this.Application_PreSendRequestHeaders));
application.PreSendRequestContent += (new EventHandler(this.Application_PreSendRequestContent));
}
private void Application_PostAcquireRequestState(object sender, EventArgs e)
{
index++;
Debug.Print($"{index}、Application_PostUpdateRequestCache\\r\\n");

}
private void Application_PostUpdateRequestCache(object sender, EventArgs e)
{
index++;
Debug.Print($"{index}、Application_PostUpdateRequestCache\\r\\n");
}
private void Application_PostReleaseRequestState(object sender, EventArgs e)
{
index++;
Debug.Print($"{index}、Application_PostReleaseRequestState\\r\\n");
}
private void Application_PostLogRequest(object sender, EventArgs e)
{
index++;
Debug.Print($"{index}、Application_PostLogRequest\\r\\n");
}
private void Application_LogRequest(object sender, EventArgs e)
{
index++;
Debug.Print($"{index}、Application_LogRequest\\r\\n");
}
private void Application_BeginRequest(object source, EventArgs e)
{
index = 0;
index++;
Debug.Print($"{index}、Application_BeginRequest\\r\\n");
}
private void Application_PostAuthorizeRequest(object sender, EventArgs e)
{
index++;
Debug.Print($"{index}、Application_PostAuthorizeRequest\\r\\n");
}
private void Application_PostAuthenticateRequest(object sender, EventArgs e)
{
index++;
Debug.Print($"{index}、Application_PostAuthenticateRequest\\r\\n");
}
private void Application_PreRequestHandlerExecute(object source, EventArgs e)
{
index++;
Debug.Print($"{index}、Application_PreRequestHandlerExecute\\r\\n");
}
private void Application_EndRequest(object source, EventArgs e)
{
index++;
Debug.Print($"{index}、Application_EndRequest\\r\\n");
}
private void Application_PostRequestHandlerExecute(object source, EventArgs e)
{
index++;

Debug.Print($"{index}、Application_PostRequestHandlerExecute\\r\\n");
}
private void Application_ReleaseRequestState(object source, EventArgs e)
{
index++;
Debug.Print($"{index}、Application_ReleaseRequestState\\r\\n");
}
private void Application_UpdateRequestCache(object source, EventArgs e)
{
index++;
Debug.Print($"{index}、Application_UpdateRequestCache\\r\\n");
}
private void Application_AuthenticateRequest(object source, EventArgs e)
{
index++;
Debug.Print($"{index}、Application_AuthenticateRequest\\r\\n");
}
private void Application_AuthorizeRequest(object source, EventArgs e)
{
index++;
Debug.Print($"{index}、Application_AuthorizeRequest\\r\\n");
}
private void Application_ResolveRequestCache(object source, EventArgs e)
{
index++;
Debug.Print($"{index}、Application_ResolveRequestCache\\r\\n");
}
private void Application_AcquireRequestState(object source, EventArgs e)
{
index++;
Debug.Print($"{index}、Application_AcquireRequestState\\r\\n");
}
private void Application_PreSendRequestHeaders(object source, EventArgs e)
{
index++;
Debug.Print($"{index}、Application_PreSendRequestHeaders\\r\\n");
}
private void Application_PreSendRequestContent(object source, EventArgs e)
{
index++;
Debug.Print($"{index}、Application_PreSendRequestContent\\r\\n");
}
}
}
C# WebService接口耗時監控,執行超過1秒鐘做日誌記錄


2、查閱相關資料瞭解到自定義事件集中在 PreRequestHandlerExecute和PostRequestHandlerExecute之間。於是編寫如下代碼實現對WebService接口方法的耗時監控。

using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.SessionState;
namespace MyService
{
public class Global : System.Web.HttpApplication
{
private const string Key = "__action_duration__";
protected void Application_Start(object sender, EventArgs e)
{
}
private void Application_PreRequestHandlerExecute(object source, EventArgs e)
{
var stopWatch = new Stopwatch();
stopWatch.Start();
Request.SetDictionary(new Dictionary<string>()
{
[Key] = stopWatch
});
}
private void Application_PostRequestHandlerExecute(object source, EventArgs e)
{
var stopwatch = Request.GetDictionary()[Key] as Stopwatch;
if (stopwatch!=null)
{
stopwatch.Stop();
var milliseconds = stopwatch.ElapsedMilliseconds;
if (milliseconds>1000)
{
string log = $"[path:{Request.Path} take {milliseconds} 毫秒]";
//超時提醒
//SystemLog.WriteInfo(log);
}
}
}

}
}
/<string>

3、Request類中並沒有SetDictionary和GetDictionary的方法,需要擴展處理。

 using System;
using System.Collections.Generic;
using System.Text;
using System.Text.RegularExpressions;
using System.Web;
using Newtonsoft.Json.Linq;
namespace MyService
{
public static class AppUtility
{
private static Dictionary<string> RequestDictionary;
public static void SetDictionary(this HttpRequest request,Dictionary<string> dic)
{
RequestDictionary = dic;
}
public static Dictionary<string> GetDictionary(this HttpRequest request)
{
return RequestDictionary;
}
}
}

/<string>/<string>/<string>


分享到:


相關文章: