0

Hoping someone can help out.

I have multiple websites running on a VM in Azure. They all access an Azure SQL database.

I have recently configured Application Insight and all things are working apart from capturing the SQL statements.

My websites are a combination of ASP.NET Core and ASP.NET.

I have followed the documents

https://learn.microsoft.com/en-us/azure/azure-monitor/app/asp-net-dependencies#advanced-sql-tracking-to-get-full-sql-query

and

https://learn.microsoft.com/en-us/azure/azure-monitor/app/application-insights-asp-net-agent?tabs=api-reference#enable-instrumentationengine

The instrumentation seems to be installed properly and seems to be working but while I get the dependency event I don't get the SQL statement which is being run.

If I query the log using this

dependencies | where success != "True" and type == "SQL" | take 10

I can see failed events but there no reason for the failure.

Looking for any help is trying to find out why the SQL statements aren't being captured or some way of fixing it.

Thanks

Greg

1 Answer 1

0

My websites are a combination of ASP.NET Core and ASP.NET.

I have tried to log SQL Dependencies for ASP.Net Framework Application.

Check the below steps:

  • I have referred this MSDoc and configure Application Insights.

  • From the MSDoc which you have shared I have added the below command in ApplicationInsights.config file under <TelemetryModules> tag.

<EnableSqlCommandTextInstrumentation>true</EnableSqlCommandTextInstrumentation>
public TelemetryClient tl;
public ActionResult Index()
{
    tl = new TelemetryClient();
 
    var conn = ConfigurationManager.ConnectionStrings["AppInsightsConnection"].ConnectionString;
    var sql = new SqlConnection(conn);
    sql.Open();

    string cmdtxt = "Select * from TestData";
    var sqlCmd = new SqlCommand(cmdtxt, sql);

    string dependencyType = "My SQL Server";
    string dependencyName = "SQL Dependency from Framework App";
    var time = DateTimeOffset.Now;

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

        var er = sqlCmd.ExecuteReader();

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

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

Transaction Search:

enter image description here

  • Check for the dependency Name and click on it.

enter image description here

Logs: enter image description here

  • For the deployed Azure App Service, make sure to enable the SQL Commands under App Service => Application Insights.

enter image description here

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.