将Azure appinsights添加到ASP.NET Framework aspx页面以记录所有SQL依赖项/sql命令文本

bsxbgnwa  于 2023-05-30  发布在  .NET
关注(0)|答案(1)|浏览(100)

我正在开发一个遗留的asp.net aspx网页应用程序。(.net framework v4.6.2)。我们希望设置Azure AppInsights日志记录来跟踪我们的应用日志。我们想要实现的最重要的方面是在App Insights中查看SQL依赖关系(查看执行/调用的SQL上下文/代码的能力)。目标:记录服务器代码正在执行的SQL命令文本。
我试着用谷歌搜索了很多文章,但大多数都是关于。net核心的,这是没有帮助的

我试着读了这篇文章:https://learn.microsoft.com/en-us/azure/azure-monitor/app/asp-net
但我仍然无法查看sql日志
我试着读了这篇文章:https://learn.microsoft.com/en-us/azure/azure-monitor/app/asp-net
添加了Azure appinsights Nuget包,添加了Log 4 net来记录跟踪信息
有时候,我甚至看不到正常的日志,比如API调用等。
任何帮助/指导都非常感谢

clj7thdc

clj7thdc1#

检查以下解决方法,以在Application Insights for Asp.Net Framework Application中跟踪SQL查询和依赖关系。

  • 我已经按照您提供的相同document自动添加应用程序洞察。
  • ApplicationInsights.config文件中添加Application Insights连接字符串。
  • 正如Application Insights中的MSDoc依赖项跟踪中所提到的,我在ApplicationInsights.config中的<TelemetryModules>下添加了以下设置。
<EnableSqlCommandTextInstrumentation>true</EnableSqlCommandTextInstrumentation>
  • 确保您的Web.config已添加以下设置。
<add name="TelemetryCorrelationHttpModule" type="Microsoft.AspNet.TelemetryCorrelation.TelemetryCorrelationHttpModule, Microsoft.AspNet.TelemetryCorrelation" />
<add name="ApplicationInsightsWebTracking" type="Microsoft.ApplicationInsights.Web.ApplicationInsightsHttpModule, Microsoft.AI.Web" />   </httpModules>
<modules>
     <remove name="TelemetryCorrelationHttpModule" />
     <add name="TelemetryCorrelationHttpModule" type="Microsoft.AspNet.TelemetryCorrelation.TelemetryCorrelationHttpModule, Microsoft.AspNet.TelemetryCorrelation" preCondition="managedHandler" />
     <remove name="ApplicationInsightsWebTracking" />
     <add name="ApplicationInsightsWebTracking" type="Microsoft.ApplicationInsights.Web.ApplicationInsightsHttpModule, Microsoft.AI.Web" preCondition="managedHandler" />  
</modules>
  • Connection string添加到Web.config的配置部分或添加到控制器本身。
<connectionStrings>
    <add name ="AppInsightsConn" connectionString="Data Source=******\\******;Initial Catalog=Test;Integrated Security=True"/>
</connectionStrings>

我在Controller中的代码:

我引用了MSDoc和12中的代码

public TelemetryClient tc;
        public ActionResult Index()
        {
            tc = new TelemetryClient();
          
            string myconn = "Data Source=******\\******;Initial Catalog=Test;Integrated Security=True";

            //var myconn = ConfigurationManager.ConnectionStrings["AppInsightsConn"].ConnectionString;
            var sqlConn = new SqlConnection(myconn);
            sqlConn.Open();

            string cmdtxt = "Select  * from MyDetails";
            var sqlCmd = new SqlCommand(cmdtxt, sqlConn);

            string dependencyType = "My SQL";
            string dependencyName = "Harshitha SQL Dependency";
            var time = DateTimeOffset.Now;

            using (var dep = tc.StartOperation<DependencyTelemetry>(dependencyName))
            {
                dep.Telemetry.Type = dependencyType;
                dep.Telemetry.Data = cmdtxt;
                dep.Telemetry.Success = true;

                var reader = sqlCmd.ExecuteReader();

                dep.Telemetry.Duration = DateTimeOffset.Now - time;
            }

            sqlConn.Close();
            return View();
        }

Application Insights中的跟踪:

依赖项:

相关问题