我正在尝试找到一种最好的方法来测量在Application Insights上记录它们的方法的持续时间,我知道如果我们做这样的事情是可能的:
public void TestMethod()
{
var sw = Stopwatch.StartNew();
//code here
sw.Stop();
Console.WriteLine("Time elapsed: {0}", sw.Elapsed);
}
Run Code Online (Sandbox Code Playgroud)
但是,正如您所想的那样,我不想在所有方法上编写它,理想情况下,我想使用装饰器,类似于此.
[MeasureTime]
public void TestMethod()
{
//code here
}
Run Code Online (Sandbox Code Playgroud)
或类似的东西.所以我的问题是:我怎样才能建立这样的东西?有没有更好的方法呢?
提前致谢!
我的场景很简单,我有一个简单的 Azure 函数,上面有 B2C 身份验证,我正在编写单元测试,但我发现了一个问题,我无法以编程方式对 azure 函数进行身份验证。
我可以通过浏览器访问,甚至可以获取令牌并将其放入单元测试中,并且工作正常,但是当我尝试使用 ClientID、TenantID 等生成令牌时,我得到了一个令牌,但是401 Azure 函数上的未经授权的响应。
有没有办法以编程方式生成有效的 B2C 令牌(无需在浏览器中登录?
到目前为止我使用的方法:
public static async Task<AuthenticationResult> GetAccessToken(string resourceUri, string clientId, string clientSecret)
{
ClientCredential clientCredential = new ClientCredential(clientId, clientSecret);
string aadInstance = "https://login.microsoftonline.com/";
string tenant = "<mytenant>.onmicrosoft.com";
string authority = string.Concat(aadInstance, tenant);
AuthenticationContext authContext = new AuthenticationContext(authority);
return await authContext.AcquireTokenAsync(resourceUri, clientCredential);
}
Run Code Online (Sandbox Code Playgroud)
我得到了一个令牌(EY.......)但无效,当我传递给Azure Function请求时,它返回401 Unauthorized。
提前致谢!伊万