欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

在 ASP.Net Core 中使用 MiniProfiler的方法

 更新時(shí)間:2021年04月08日 15:50:11   作者:碼農(nóng)讀書  
這篇文章主要介紹了在 ASP.Net Core 中使用 MiniProfiler的方法,本文給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下

web應(yīng)用程序的性能相信是大家普遍關(guān)心的一個(gè)問題,也相信大家有很多工具可用來分析應(yīng)用程序的性能并能夠找到其中的瓶頸,MiniProfiler 就是這個(gè)領(lǐng)域中的一款產(chǎn)品,它是一款簡單的,功能強(qiáng)大的web應(yīng)用分析工具,MiniProfiler 可用來幫助我們找到 慢查詢, 慢響應(yīng) 等問題。

MiniProfiler 可用在 Asp.NetASP.Net Core 中,這篇文章將會(huì)討論如何使用 MiniProfiler,并通過它找到應(yīng)用程序的性能問題。

安裝 MiniProfiler

要想使用 MiniProfiler,需要通過 nuget 引用 MiniProfiler.AspNetCore.Mvc 包,可以通過 Visual Studio 2019 的 NuGet package manager 可視化界面安裝 或者 通過 NuGet package manager 命令行工具輸入以下命令:

dotnet add package MiniProfiler.AspNetCore.Mvc

安裝好之后,接下來就要將 MiniProfiler 注入到 ServiceCollection 容器中,如下代碼所示:

 // This method gets called by the runtime. Use this method to add services to the container.
    public void ConfigureServices(IServiceCollection services)
    {
      services.AddControllersWithViews();

      services.AddMiniProfiler(options => options.RouteBasePath = "/profiler");
    }

注入好之后,接下來就需要使用 UseMiniProfiler 擴(kuò)展方法將其注入到 Request Pipeline 管道中,如下代碼所示:

 public void Configure(IApplicationBuilder app, IWebHostEnvironment env, ILoggerFactory loggerFactory)
    {
      app.UseMiniProfiler();

      app.UseEndpoints(endpoints =>
      {
        endpoints.MapControllerRoute(
          name: "default",
          pattern: "{controller=Home}/{action=Index}/{id?}");
      });
    }

然后在 _Layout.cshtml 頁面中增加如下兩行命令。

@using StackExchange.Profiling
@addTagHelper *, MiniProfiler.AspNetCore.Mvc

最后需要在 WebPage 中指定 MiniProfiler 分析窗口應(yīng)該顯示的位置,那如何做呢? 在 body 標(biāo)簽內(nèi)使用 mini-profiler 標(biāo)記,如下代碼所示:

<mini-profiler position="@RenderPosition.Right" max-traces="5" />

在 ASP.Net Core MVC 中使用 MiniProfiler

MiniProfiler 會(huì)提供 頁面加載時(shí)間數(shù)據(jù)庫查詢性能指標(biāo),接下來把程序跑起來,你會(huì)看到如下的性能指標(biāo)圖。

有些朋友可能就要問了,大體時(shí)間我是知道了,那如果我只想獲取某一指定代碼塊的執(zhí)行時(shí)間呢? 當(dāng)然也是可以的,下面的代碼展示了如何去實(shí)現(xiàn)。

 public class HomeController : Controller
  {
    ILogger<HomeController> logger;

    public HomeController(ILogger<HomeController> logger)
    {
      this.logger = logger;
    }

    public IActionResult Index()
    {
      var miniProfiler = MiniProfiler.Current;
      List<Author> authors = new List<Author>();

      miniProfiler.RenderIncludes(this.HttpContext);

      using (miniProfiler.Step("Get Authors"))
      {
        authors.Add(new Author() { Id = 1, FirstName = "Joydip", LastName = "Kanjilal", Address = "Hyderabad, India" });
        authors.Add(new Author() { Id = 2, FirstName = "Stephen", LastName = "Smith", Address = "NY, USA" });
        authors.Add(new Author() { Id = 3, FirstName = "Anand", LastName = "Narayanan", Address = "Chennai, India" });
        authors.Add(new Author() { Id = 4, FirstName = "Steve", LastName = "Jones", Address = "London, UK" });
      }
      return View(authors);
    }
  }

  public class Author
  {
    public int Id { get; set; }
    public string FirstName { get; set; }
    public string LastName { get; set; }
    public string Address { get; set; }
  }

從上面的代碼中可以看到,我用 using (miniProfiler.Step("Get Authors")) 做了語句塊標(biāo)記,理論上 mini-profile 窗口上應(yīng)該有類似 Get Authors 指標(biāo)欄,接下來把程序跑起來,一起來看看效果。

除了順向操作,你也可以指定讓某些代碼塊不要顯示在 mini-profile 中,需要做的是調(diào)用 Ignore() 即可,如下代碼所示:

using (MiniProfiler.Current.Ignore())
{
 // Write code here that you don't
 // want MiniProfiler to profile
}

使用 MiniProfile 分析 ADO.NET 查詢

除了做一些常規(guī)的頁面分析,還可以直接對 ADO.NET 查詢性能進(jìn)行分析,這就🐂👃了,要這么做的話,需要使用 ProfileDbConnectionProfileDbCommand 即可,如下代碼所示:

   public IActionResult Index()
    {
      using (SqlConnection connection = new SqlConnection(@"Data Source=.; Initial Catalog=PYZ_L; Trusted_Connection=Yes"))
      {
        using (ProfiledDbConnection profiledDbConnection = new ProfiledDbConnection(connection, MiniProfiler.Current))
        {
          if (profiledDbConnection.State != System.Data.ConnectionState.Open)
          {
            profiledDbConnection.Open();
          }

          using (SqlCommand command = new SqlCommand("Select * From Clothes", connection))
          {
            using (ProfiledDbCommand profiledDbCommand = new ProfiledDbCommand(command, connection, MiniProfiler.Current))
            {
              var data = profiledDbCommand.ExecuteReader();
              //Write code here to populate the list of Authors
            }
          }
        }
      }

      return View();
    }

從上圖可以看到,確實(shí)對 ADO.NET 查詢有著清晰的分析,相信在幫助大家分析問題時(shí)很有幫助。

MiniProfiler 是一個(gè)可應(yīng)用于 .NET, Ruby, Go 和 Node.js 的性能分析工具,你可以使用 MiniProfiler 去分析 Dapper,Linq2SQL,Entity Framework 所使用的sql的查詢性能,此外 MimiProfile 之所以 Mini,意味著它介入到你的應(yīng)用程序中所帶來的性能開銷微乎其微,所以大家可放心的丟到生產(chǎn)上去吧!

到此這篇關(guān)于在 ASP.Net Core 中使用 MiniProfiler的方法的文章就介紹到這了,更多相關(guān) ASP.Net Core 使用 MiniProfiler內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • aspx中頁面按鈕寫返回上一頁代碼

    aspx中頁面按鈕寫返回上一頁代碼

    近段時(shí)間,Insus.NET不斷補(bǔ)學(xué)習(xí)Javascript知識。但在練習(xí)寫返回上一頁的功能時(shí),卻遇上一點(diǎn)小問題
    2012-11-11
  • ASP .NET Core API發(fā)布與部署以及遇到的坑和解決方法

    ASP .NET Core API發(fā)布與部署以及遇到的坑和解決方法

    這篇文章主要介紹了ASP .NET Core API發(fā)布與部署以及遇到的坑和解決方法,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2020-08-08
  • asp.net(C#)生成Code39條形碼實(shí)例 條碼槍可以掃描出

    asp.net(C#)生成Code39條形碼實(shí)例 條碼槍可以掃描出

    這篇文章主要介紹了asp.net(C#)生成Code39條形碼實(shí)例 條碼槍可以掃描出。需要的朋友可以過來參考下,希望對大家有所幫助
    2014-02-02
  • .NetCore基礎(chǔ)之讀取配置文件詳解

    .NetCore基礎(chǔ)之讀取配置文件詳解

    在應(yīng)用程序開發(fā)中,配置文件是主要存儲系統(tǒng)的初始配置信息,配置文件的讀取雖然屬于基礎(chǔ)內(nèi)容卻又經(jīng)常用到。本文將詳細(xì)為大家介紹.Net?Core?如何讀取配置文件的,需要的可以參考一下
    2022-03-03
  • C# winform打印excel的方法

    C# winform打印excel的方法

    這篇文章主要為大家詳細(xì)介紹了C# winform打印excel的方法,使用NPOI+Spire.xls+PrintDocument直接打印excel,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2017-11-11
  • 關(guān)于visual studio 2012 update 2中的新功能介紹

    關(guān)于visual studio 2012 update 2中的新功能介紹

    本篇文章小編為大家介紹,關(guān)于visual studio 2012 update 2中的新功能介紹說明。需要的朋友參考下
    2013-04-04
  • Nlog日志框架集成Seq擴(kuò)展包

    Nlog日志框架集成Seq擴(kuò)展包

    這篇文章介紹了Nlog日志框架集成Seq擴(kuò)展包的方法,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2022-02-02
  • C#.net 微信公眾賬號接口開發(fā)

    C#.net 微信公眾賬號接口開發(fā)

    這篇文章主要介紹了C#.net 微信公眾賬號接口開發(fā),需要的朋友可以參考下
    2016-05-05
  • asp.net下將頁面內(nèi)容導(dǎo)入到word模板中的方法

    asp.net下將頁面內(nèi)容導(dǎo)入到word模板中的方法

    asp.net下將頁面內(nèi)容導(dǎo)入到word模板中的方法,需要的朋友可以參考下。
    2010-10-10
  • asp.net中如何實(shí)現(xiàn)水印

    asp.net中如何實(shí)現(xiàn)水印

    水印的實(shí)現(xiàn)方法有很多,而且各種各樣,在本文將為大家介紹下在asp.net中時(shí)如何實(shí)現(xiàn)的,如果你不會(huì)可以參考下
    2013-09-09

最新評論