![](/img/trans.png)
[英]Using Log4Net to log all the functions called in ASP.NET and C# application
[英]Identify execution time and Tracing of each API in ASP.NET WEB API and log to files using log4net using C#
當前,我正在使用log4net捕獲每個API的執行時間,並在每個API的開始和結束處明確提及開始時間和結束時間,並寫入日志文件,如下所示
[HttpGet]
Public IHttpActionResult GetProducts()
{
Log.info("API Execution Started at " + DateTime.Now());
Var Products = UnitOfWork.GenericRepo.GetProducts();
Log.Info("API Execution Completed at "+ DateTime.Now());
Return Ok(Products);
}
有沒有更好的方法來捕獲每個API的執行時間並將其寫入日志文件? 另外,請提供我有關如何捕獲所有API的跟蹤信息並將其寫入不同日志文件的見解。 例如,應將每個API的執行時間寫入“ ExecutionLog.txt”,並將跟蹤信息寫入“ TracingLog.txt”。 我還應該能夠在運行時啟用或禁用此功能。
提前致謝
@Mahesh,您不需要在每個API中編寫它。 您可以使用ActionFIlter和ResultFilter並編寫一個代碼塊來跟蹤API中的所有操作
看看這個https://jmperezperez.com/tracking-action-execution-time-in-asp-net-mvc/
在System.Diagnostics
命名空間中,您有一個名為StopWatch
的類。 您可以使用它來檢查功能執行時間。 使用Start()
和Stop()
方法來查找總執行時間。
這是從dotnet pearl網站上獲取的一個簡單示例
Stopwatch stopwatch = new Stopwatch();
// Begin timing.
stopwatch.Start();
// Do something.
for (int i = 0; i < 1000; i++)
{
Thread.Sleep(1);
}
// Stop timing.
stopwatch.Stop();
// Write result.
Console.WriteLine("Time elapsed: {0}", stopwatch.Elapsed);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.