How to track down AppInsights internal error - c#

Some general setup of my system:
Windows x64
.net 3.1
AspNet.Core
Serilog + AppInsights writer
Recently, we started to observe many trace logs like below. I think something went wrong when we try to write log into AppInsights. But we can see our logs. It seems we have everything we wanted logging into AppInsights correctly. But we also have this unwanted trace message logged in as well, and there are a lot of them.
This trace message happens on my local machine, in our Azure AKS environment. But the stack trace is too short to help locating the origin of the error.
AI (Internal): [Microsoft-ApplicationInsights-Core] [msg=Log Error];[msg=Exception while initializing Microsoft.ApplicationInsights.AspNetCore.TelemetryInitializers.ClientIpHeaderTelemetryInitializer, exception message - System.ObjectDisposedException: Request has finished and HttpContext disposed.
Object name: 'HttpContext'.
at Microsoft.AspNetCore.Http.DefaultHttpContext.ThrowContextDisposed()
at Microsoft.AspNetCore.Http.DefaultHttpContext.get_Features()
at Microsoft.ApplicationInsights.AspNetCore.TelemetryInitializers.TelemetryInitializerBase.Initialize(ITelemetry telemetry)
at Microsoft.ApplicationInsights.TelemetryClient.Initialize(ITelemetry telemetry)]

I tried to create an environment, with Asp.net core application & .net3.1 for track down the logs (Request, exceptions, users, failure, trace etc.)
Here are the steps i followed : Added application insight telemetry to my application and install the NuGet package Microsoft.ApplicationInsights.AspNetcore
And in startup.cs under services.AddControllersWithViews(); add
services.AddApplicationInsightsTelemetry(Configuration["APPINSIGHTS_CONNECTIONSTRING"]);
After deploy the app to azure and for tracing the logs go to my created Application insights> Transaction search>view data ,Where we can see all of our logs as below also to identify if there is any error occurs.
Also we can download all our logs and track down for the application using kudu-console.
Go to App service>Advance tools>Go
For more information please refer the below links:
.MS DOC | Explore .NET/.NET Core and Python trace logs in Application Insights
.SO Thread | Application Insights not showing stack trace for errors
.Blog| Using Azure Application Insights For Exception Logging In C#

Related

Is there a way to hide debug logging of a package in Visual Studio C#?

I am writing a DotNetCore 3.1 Blazer Server Side application. One of the required modules is an OPC UA Client wrapper dll I wrote around OPC Foundation's NetStandard implementation. This wrapper does not log anything. All logging appears to be done by the OPC Foundation.
We make around 25 read/write operations per second. Each operations logs data like so:
8/2/2021 10:42:03.808 Read Completed. RequestHandle=204, PendingRequestCount=0
8/2/2021 10:42:04.501 Read Called. RequestHandle=208, PendingRequestCount=1
8/2/2021 10:42:04.503 Bytes written: 136
This spams the debug log, a problem for obvious reasons.
Ideally I would like to make my dll not log these operations, but a solution to mute them in Visual Studio 2019 would suffice.
You can configure what the OPCF NetStandard stack is logging by calling Utils.SetTraceMask()
Level
Value
Usage
None
0x0
Not a useable level for logging itself, just for user to deactivate logging
All
0x7FFFFFFF
Output all messages
Security
0x200
Basic Messages related to security
ExternalSystem
0x100
Not used within stack
StartStop
0x80
Messages related to application initialization or shutdown
OperationDetail
0x40
Detailed Messages for each operation
Operation
0x20
Basic Messages for each operation
ServiceDetail
0x10
Detailed Messages for service calls
Service
0x8
Basic Messages for service calls
StackTrace
0x4
Output stack traces
Information
0x2
Output informational messages
Error
0x1
Output error messages
Note: to use the stack logging it is required to call Utils.SetTraceOutput(Utils.TraceOutput.DebugAndFile) and Utils.SetTraceLog(null, false) because up to now, the stack don’t support TraceOutput.DebugOnly logging and logging into file is unusable (open, modify and close trace file for each log entry).
The good news is that the team works on improving the tracing/logging (see UA-.NETStandard#1262.

Where can I find docker container logs for Azure App Service

I do have a Docker container running a .net core 2 app.
The logging is configured using this code in Program.cs
public static IWebHostBuilder CreateWebHostBuilder(string[] args) =>
WebHost.CreateDefaultBuilder(args)
.ConfigureLogging((hostingContext, logging) =>
{
logging.AddConfiguration(hostingContext.Configuration.GetSection("Logging"));
logging.AddConsole();
logging.AddDebug();
})
.UseStartup<Startup>();
and the appsettings.json file
{
"Logging": {
"LogLevel": {
"Default": "Information"
}
},
}
Logging seems to be Ok, when running Kestrel directly, I can see the logs in the terminal. Same thing, when containerized: the command docker logs shows what I want.
Troubles arise in production, when run in as a container in Azure Web App. I cannot find any consistent docker logs.
I made attempts to visit the Log file via FTP or via the url https://[mysite].scm.azurewebsites.net/api/logs/docker the log files are almost empty for example,
https://[mysite].scm.azurewebsites.net/api/vfs/LogFiles/2018_09_09_RD0003FF74F63E_docker.log,
only container starting lines are present
Also I do have the same lines in the usual portal interface.
The question is: do docker logs are automatically output in docker.log files in Azure Web App? Is there something that I am missing?
Firstly you need to enable container logs
[App Service] -> Monitoring -> App Service logs
Then you can see container logs in [App Service] -> Monitoring -> Log stream
UPD
Also you can find logs in KUDU
Then "Current Docker Logs"
I've been cursing this for a good while, and eventually found something that works for me.
First I enabled filesystem logging as per #dima_horror's answer.
Next I ran a command-line az webapp log tail --name myApp --resource-group myRg
That now seems to give me useful output (it gave me nothing prior to enabling filesystem logging).
Have you checked the logs in container settings? I followed this guide to deploy a container to Azure web app.
To add to the answers, if you're having trouble with deploying the image in your App Service and need logs for that specifically, you can get the docker logs for the provisioning phase of your container under Deployment Center, e.g.:
I have the same exact issue, the App Service container logs is generic and vague. This is not the same logs that Docker shows us whenever we run a container.
17/02/2020 08:59:25.186 INFO - Site: tutorial-api - Start container succeeded. Container: f8bfa7e27680c0e9551c6157f9d1c8a73c9a3e739b4f15de8586ce52809798d3
17/02/2020 08:59:30.675 INFO - Site: tutorial-api - Application Logging (Filesystem): On
17/02/2020 08:59:44.106 INFO - Site: tutorial-api - Waiting for container to be ready
17/02/2020 08:59:49.116 INFO - Site: tutorial-api - Container has exited
17/02/2020 08:59:49.117 ERROR - Site: tutorial-api - Container could not be started
17/02/2020 08:59:49.120 INFO - Site: tutorial-api - Purging after container failed to start
17/02/2020 08:59:49.120 ERROR - Site: tutorial-api - Unable to start container. Error message: Container could not be started: tutorial-api_20
"Unable to start container, container couldn't be started"
Wow! Azure just told me every 60 seconds a minute has passed.
I understand that this is production environment, but you got to give us something!
Out of frustration, I decided to run same image in a Azure Container Instance resource, there it shows you the same detailed logs that Docker provides (see screenshot below)
Now that's what I'm talking about!
Using the error logs in Azure Container Instance, I found out that my App Service couldn't access the Sql Server resource (even though they are within the same resource group). I simply enabled the Sql Server resource to be accessed within the same resource group

Application Insights Exceptions and Failures

I'm trying to work with Application Insights in the Azure preview portal and with Visual Studio 2015 Community edition. I'm encountering three different problems which I suspect are all related. I'm wondering if others are encountering these issues.
In one of my test web applications, the App-Insight tool is partially working. In another I'm getting errors concerning irretrievable data. In the third, I'm running into deployment failures when I use App-Insight with resources in a new resource group.
I will present all three in this question, because I suspect they may all have the same root cause.
Partial Results
The very first app I tried to retrofit with app-insight does actually work and display app-insight telemetry...or at least it mostly works. Half the telemetry data shows, and the rest does not make it.
The sequence of setting this up was as follows:
I started by installing the sample Mobile-MVC app that came with this MSDN Magazine article, from June 2015. After fiddling around with adding bootstrap to make responsive pages, I then decided to commit the code to VS online GIT and then deploy as an Azure web app.
I then followed instructions to View logs in Application Insights and ran the application which succeeds in launching a browser and allowing me to interact with the site. However, during this exercise I get tons of exceptions showing in the Visual Studio output window. I've included a small snippet here, where I've bolded the most interesting two pieces:
Exception thrown: 'System.MethodAccessException' in Microsoft.ApplicationInsights.Extensibility.DependencyCollector.dll
Exception thrown: 'System.TypeInitializationException' in Microsoft.ApplicationInsights.Extensibility.DependencyCollector.dll
Exception thrown: 'System.TypeInitializationException' in Microsoft.ApplicationInsights.Extensibility.DependencyCollector.dll
ApplicationInsightsHttpModule failed at initialization with exception: System.TypeInitializationException: The type initializer for 'Microsoft.ApplicationInsights.Extensibility.DependencyCollector.Tracing.DependencyCollectorEventSource' threw an exception. ---> System.MethodAccessException: Attempt by method 'Microsoft.ApplicationInsights.Extensibility.DependencyCollector.Tracing.DependencyCollectorEventSource..ctor()' to access method 'Microsoft.ApplicationInsights.Extensibility.Implementation.Tracing.ApplicationNameProvider..ctor()' failed.
at Microsoft.ApplicationInsights.Extensibility.DependencyCollector.Tracing.DependencyCollectorEventSource..ctor()
at Microsoft.ApplicationInsights.Extensibility.DependencyCollector.Tracing.DependencyCollectorEventSource..cctor()
--- End of inner exception stack trace ---
at Microsoft.ApplicationInsights.Extensibility.DependencyCollector.DependencyTrackingTelemetryModule.Initialize(TelemetryConfiguration configuration)
at Microsoft.ApplicationInsights.Extensibility.Implementation.TelemetryConfigurationFactory.InitializeComponents(IEnumerable components, TelemetryConfiguration configuration)
at Microsoft.ApplicationInsights.Extensibility.Implementation.TelemetryConfigurationFactory.InitializeComponents(TelemetryConfiguration configuration)
at Microsoft.ApplicationInsights.Extensibility.Implementation.TelemetryConfigurationFactory.Initialize(TelemetryConfiguration configuration)
at Microsoft.ApplicationInsights.Extensibility.TelemetryConfiguration.get_Active()
at Microsoft.ApplicationInsights.Extensibility.Web.ApplicationInsightsHttpModule..ctor()
[msg=Log Error];[msg=System.TypeLoadException: Could not load type 'Microsoft.ApplicationInsights.Channel.JsonSerializer' from assembly 'Microsoft.ApplicationInsights, Version=1.2.0.5639, Culture=neutral, PublicKeyToken=31bf3856ad364e35'.
at Microsoft.ApplicationInsights.Extensibility.Web.Implementation.TelemetrySerializer.d__0.MoveNext()
at System.Runtime.CompilerServices.AsyncTaskMethodBuilder.Start[TStateMachine](TStateMachine& stateMachine)
at Microsoft.ApplicationInsights.Extensibility.Web.Implementation.TelemetrySerializer.SerializeAsync(IEnumerable1 items)
at Microsoft.ApplicationInsights.Extensibility.Web.Implementation.TelemetryBuffer.<>c__DisplayClass5.<FlushAsync>b__3()
at System.Threading.Tasks.Task1.InnerInvoke()
at System.Threading.Tasks.Task.Execute()
--- End of stack trace from previous location where exception was thrown ---
at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
at Microsoft.ApplicationInsights.Extensibility.Web.Implementation.TelemetryBuffer.d__7.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
at Microsoft.ApplicationInsights.Extensibility.Implementation.TaskTimer.<>c__DisplayClass1.<b__0>d__3.MoveNext()]
My Azure portal is receiving telemetry on some things, but not others. In short, the ability to produce telemetry is crippled, as suggested by the above exceptions.
Now for the next problem.
New Resource Group
Using the preview portal, I created a brand new resource group called "SandBox". Again using the preview portal, I then made a brand new web application called "ScratchWeb" which I assigned to the "Sandbox" resource group.
The deployment of the web resource eventually completes, but there is an associated application insight resource called ""Microsoft.WebSite3cb98c31-893c"" that automatically is created which experiences a dozen or so time-out errors before it eventually is declared as a failed deployment. Below I show an example of one of the timeout errors, as well as the final error which gives some key information.
Operation Id
1E2094CECE6531F1
Tracking Id
31283062-9061-4dcf-bd42-f9885abbf596
Status
GatewayTimeout
Provisioning State
Running
Timestamp
‎9‎/‎28‎/‎2015‎ ‎10‎:‎37‎:‎59‎ ‎PM
Duration
PT30.7117477S
Type
microsoft.insights/components
Resource Id
/subscriptions/da769a1f-78d8-4d45-b9b3-c15ea0c89ca2/resourceGroups/SandBox/providers/microsoft.insights/components/ScratchWeb
StatusMessage
{"code":"GatewayTimeout","message":"Request timed out","innererror":{"diagnosticcontext":"034b642f-aece-494b-9ede-f8b015f5bfeb","time":"2015-09-29T05:37:59.8355234Z","data":"d0fe44bb-8317-4359-bf2c-160f1eb00433"}}
Resource
ScratchWeb
And here is the final failure error message:
Operation Id
1E2094CECE6531F1
Tracking Id
08dbbf0a-0d4a-4692-9b1a-3603a984d40d
Status
Conflict
Provisioning State
Failed
Timestamp
‎9‎/‎28‎/‎2015‎ ‎10‎:‎48‎:‎16‎ ‎PM
Duration
PT10M47.5300298S
Type
microsoft.insights/components
Resource Id
/subscriptions/da769a1f-78d8-4d45-b9b3-c15ea0c89ca2/resourceGroups/SandBox/providers/microsoft.insights/components/ScratchWeb
StatusMessage
{"code":"The component already existed in a different resource group.","message":"The component already existed in a different resource group.","innererror":{"diagnosticcontext":"9ea42d3d-4dbf-46e8-a188-eac09a4ddb09","time":"2015-09-29T05:48:15.7282268Z"}}
Resource
ScratchWeb
Is this because the "Application Insights" preview is not capable of running in more than one resource group, or is it because my subscription doesn't allow it?
The weirdest part is that once the deployment of the app-insights resource FINALLY failed, it nevertheless seems to have been created and is not visible inside my Application Insights resource collection.
Now the last problem...
Original Resource Group
If I try to create a new web site in my existing, default web app resource group...then none of the aforementioned problems occurs. Unfortunately I get another problem. The "application insight" resource blade tells me "error retrieving data" when it tries to show me charts. On the other hand, Visual Studio tells me that everything is fine...it says something like "congratulations, you have published you first app insight data."
So, in the end, I have only one web application with viewable App-Insight results, and those results are crippled. My attempt to create a 2nd working web app with app-insight was unsuccessful regardless if I used an existing resource group or tried a new one.
With Visual Studio 2015 I used two different approaches to try adding the App-Insight SDK. I tried adding it to my web project after-the-fact, and I also tried adding it from a new project template. The only scenario where I got anything to work is when I instrumented my first web app "after-the-fact" with the App-Insight SDK. Unfortunately (1) those results were crippled and (2) as I've described above, I can't achieve even "crippled" results a second time. Thus right now the App-Insight capability is unworkable for me.
Try removing all references to Application Insights, Clean the project. Then add the NuGet package Microsoft.ApplicationInsights.Web, currently at version 1.2.1.
PM> Install-Package Microsoft.ApplicationInsights.Web
This will install all the required dependencies. Once installed, create an Application Insights resource in your subscription (follow instructions at Add Application Insights SDK to monitor your ASP.NET app) and add the Instrumentation key to your app. I'd recommend putting the instrumentation key in your web.config settings:
<appSettings>
<add key="AppInsightsInstrumentationKey" value="your-key-value" />
</appSettings>
and adding it to your application in Application_Start like:
TelemetryConfiguration.Active.InstrumentationKey = ConfigurationManager.AppSettings["AppInsightsInstrumentationKey"] ?? "some-default-key";
UPDATE ---
There is more troubleshooting information here.
If browser events are not being logged:
If there is a firewall in front of your browser client, you might have to open TCP ports 80 and 443 for outgoing traffic to dc.services.visualstudio.com and f5.services.visualstudio.com.
In your browser developer tools, look for the "track" entry on the network tab - it should be posting data to https://dc.services.visualstudio.com/v2/track (or fc...)
If server data is not recording:
Install Application Insights via the Azure Portal: browse to your WebApp >> Tools >> Extensions, then install the Application Insights extension.

diagnostics not working in web role for Azure SDK 2.5.1

I am working with Azure SDK 2.5.1, mainly on the new designed diagnostics stuffs. However, I found I cannot get it run for my web role.
So, I created a cloud service project, added a web role. Then, I appended one Trace message at the end of Application_Start in Global.asax.cs:
Trace.TraceInformaction("Application_Start end.");
After that, I right clicked the WebRole and opened the Properties tab. In the diagnostics config window:
General: I choose 'Custom plan', also specified the storage account, keep the 'Disk Quota in MB' as default '4096'
Application Logs: 'Log level' switch to 'All', others kept as default
Other tabs are in default config settings
After I deployed the project to cloud, I found some unexpected things:
There is no WADLogsTable exists in Table storage. That's very strange, if I use a Worker Role, it would work as expected. So in web role, I just cannot find the Trace logging?
For the performance counters, since I am using the default config with 8 counters, I can only see 8 in WADPerformanceCountersTable table storage. In my assumption, over time, there would be more and more values of this 8 counters transferred to this table. But it was just not happened, after several hours, it still had that 8 counter values.
I just thought the diagnostics for Web Role just crashed or not working at all. Meanwhile, I checked the logs located at "C:\Logs\Plugins\Microsoft.Azure.Diagnostics.PaaSDiagnostics\1.4.0.0\DiagnosticsPlugin.log" in server, at the end of this file there is an exception said some diagnostics process exit:
DiagnosticsPlugin.exe Error: 0 : [4/25/2015 5:38:21 AM] System.ArgumentException: An item with the same key has already been added.
at System.Collections.Generic.Dictionary`2.Insert(TKey key, TValue value, Boolean add)
at Microsoft.Azure.Plugins.Diagnostics.dll.PluginConfigurationSettingsProvider.LoadWadXMLConfig(String fullConfig)
DiagnosticsPlugin.exe Error: 0 : [4/25/2015 5:38:21 AM] Failed to load configuration file
DiagnosticsPlugin.exe Information: 0 : [4/25/2015 5:38:21 AM] DiagnosticPlugin.exe exit with code -105
From MSDN, the code -105 means:
The Diagnostics plugin cannot open the Diagnostics configuration file.
This is an internal error that should only happen if the Diagnostics
plugin is manually invoked, incorrectly, on the VM.
It doesn't make sense, I did nothing to the VM.
As I said above, I just did very tiny changes to the scaffold code generated by Visual Studio 2013. Did I do something wrong or it's a bug for Azure SDK 2.5?
By the way, it seems everything is ok for Worker Role.
Quote from MSDN forum:
Hey folks jumping it late here but just wanted to confirm that there was an issue with the initial 1.4.0.0 diagnostics extension where the plugin could crash on the web role under certain circumstances.
The issue has since been resolved with 1.4.1.0. If you re-apply the extension on the role you should automatically get the latest version which fixes the issue.
At early stage, the diagnostics in SDK 2.5.1 has some internal issue inside, but Microsoft already fixed it.
Anyway, we have newer version can be applied, so this is not a problem anymore, just in case you meet this issue too.

Enterprise library Logging application block fails to write to database

In my application I am using Enterprise library logging application block for logging exception to DB.Also, I am using fluent API to configure the logging application block.
Things I noticed:
When I was not using the Fluent API and database logging fails it logged the exception to windows event log.(version 5.0)
But when i used it(Fluent API), In case of database failure it is not logging exception anywhere not even in windown event log.
My Question:
Is it a normal behavior of Enterprise library logging application block?
Is there any way that using fluent API I can get the functionality what I was getting without it, Meaning (in case db logging fails log to windows event log).
Feel free to suggest in case of any discrepancy. :-)
The exact syntax depends on your existing configuration. Assuming you have no trace listener or formatter already configured that you want to use:
configurationSourceBuilder
.ConfigureLogging()
.SpecialSources.LoggingErrorsAndWarningsCategory
.SendTo.EventLog("Event Log Listener")
.FormatWith(new FormatterBuilder().TextFormatterNamed("Text Formatter"));
If you already have an Event Log trace listener configured that you want to use (named "Event Log Listener" in this example):
configurationSourceBuilder
.ConfigureLogging()
.SpecialSources.LoggingErrorsAndWarningsCategory
.SendTo.SharedListenerNamed("Event Log Listener");
If you already have a log formatter configured that you want to use (named "Text Formatter" in this example):
configurationSourceBuilder
.ConfigureLogging()
.SpecialSources.LoggingErrorsAndWarningsCategory
.SendTo.EventLog("Event Log Listener")
.FormatWithSharedFormatter("Text Formatter");

Categories