I am trying to add a cloud_RoleName to my Application Insights telemetry in a .NET 4.8 Framework Windows Service (e.g. Console App, etc.). I am using the nuget package Microsoft.ApplicationInsights.WindowsServer and an ApplicationInsights.config to configure everything but I just can't get the RoleName to be populated.
I tried using this:
<TelemetryInitializers>
<Add Type="Microsoft.ApplicationInsights.WindowsServer.AzureRoleEnvironmentTelemetryInitializer, Microsoft.AI.WindowsServer"/>
</TelemetryInitializers>
but it causes an exception:
Unexpcted Exception During Runtime Startup:
System.TypeInitializationException: The type initializer for '<Module>' threw an exception. ---> <CrtImplementationDetails>.ModuleLoadException: The C++ module failed to load while attempting to initialize the default appdomain.
---> System.Runtime.InteropServices.COMException: Invalid operation. (Exception from HRESULT: 0x80131022)
at System.Runtime.InteropServices.Marshal.ThrowExceptionForHRInternal(Int32 errorCode, IntPtr errorInfo)
at <CrtImplementationDetails>.GetDefaultDomain()
at <CrtImplementationDetails>.DoCallBackInDefaultDomain(IntPtr function, Void* cookie)
at <CrtImplementationDetails>.LanguageSupport.InitializeDefaultAppDomain(LanguageSupport* )
at <CrtImplementationDetails>.LanguageSupport._Initialize(LanguageSupport* )
at <CrtImplementationDetails>.LanguageSupport.Initialize(LanguageSupport* )
--- End of inner exception stack trace ---
at <CrtImplementationDetails>.LanguageSupport.Initialize(LanguageSupport* )
at .cctor()
--- End of inner exception stack trace ---
at Microsoft.WindowsAzure.ServiceRuntime.RoleEnvironment.InitializeEnvironment()
at Microsoft.WindowsAzure.ServiceRuntime.RoleEnvironment..cctor()
I am using several other applications all running on the same environment and logging to the same InstrumentationKey, but without the RoleName filled in its hard to delineate what app is logging what data and my Application map looks like this:
Is using the RoleName the correct approach here, and if so then how do I get AzureRoleEnvironmentTelemetryInitializer to behave?
--==UPDATE==--
I managed to fix the exception via useLegacyV2RuntimeActivationPolicy="true" in the program's app.config. And now I have a new error:
Microsoft.WindowsAzure.ServiceRuntime Information: 100 : Role environment . INITIALIZING
Microsoft.WindowsAzure.ServiceRuntime Information: 100 : Role environment . INITIALED RETURNED. HResult=-2147024894
Microsoft.WindowsAzure.ServiceRuntime Error: 102 : Role environment . FAILED TO INITIALIZE. hr: -2147024894
Error -2147024894 appears to be a "File not found" kind of error
The cloud RoleName is still empty
OK, I have a solution for getting AzureRoleEnvironmentTelemetryInitializer to load without errors now, but unfortunately it doesn't seem to populate the cloud RoleName so I had to try something else (below).
The fixes were:
1 - Special attribute in app.config
<startup useLegacyV2RuntimeActivationPolicy="true">
2 - Special Environment variable must be defined
Environment.SetEnvironmentVariable("WEBSITE_SITE_NAME", "UndocumentedEnvironmentVariablesAreNotGreat");
and this helps AzureRoleEnvironmentTelemetryInitializer load (no more errors), but does not solve my actual problem of the empty RoleName.
I had to add some custom code to get this to work:
public class MyWebTelemetryInitializer : ITelemetryInitializer
{
public void Initialize(ITelemetry telemetry)
{
telemetry.Context.Cloud.RoleName = "MyRoleName";
}
}
And then add the custom ITelemetryInitializer into ApplicationInsights.config:
<TelemetryInitializers>
<Add Type="MyNamespace.MyWebTelemetryInitializer, FrameworkConsoleApp3"/>
...
</TelemetryInitializers>
and now the "ai.cloud.role":"MyRoleName" is in the Telemetry = Joy! This seems to fix the "Application map" too - I no longer have a node with calls to itself as per the image above!
I am still interested in how to do this the "correct way", if anyone can tell me I would appreciate!
User contributions licensed under CC BY-SA 3.0