How to load custom events with Azure Monitor SDK? - azure

I use Microsoft.Azure.Management.Fluent SDK package for accessing Azure Monitor metrics like this:
var credentials = SdkContext.AzureCredentialsFactory
.FromServicePrincipal("some-client-id", "some-client-secret", "some-tenant-id", AzureEnvironment.AzureGlobalCloud);
var azure = Microsoft.Azure.Management.Fluent.Azure
.Configure()
.Authenticate(credentials)
.WithSubscription("my-subscription");
foreach (var metricDefinition in azure.MetricDefinitions.ListByResource("my-resource-uri"))
{
...
}
It works fine for common metrics (Application Insights standard metrics). But how can I get my custom events with this SDK to render them in my custom app dashboard? I see recorded events in Application Insights dashboard under "Log-based metrics", but ListByResource method doesn't return them.

I suspect this may be a bug.
The method ListByResource has 2 parameters, the 2nd one is metricnamespace.
By default, the metricnamespace is microsoft.insights/components, which is used for Application Insights standard metrics.
For Log-based metrics, the metricnamespace should be microsoft.insights/components/kusto(to check the value, you can use tools like Fiddler or developer tool of any browser). As per my test, when use ListByResource("resourceId","microsoft.insights/components/kusto"), it throws a "bad request" error. So I raised an issue to track this.

Related

Console application Logs to store in Azure storage

I am using a console application that runs on on-premise servers triggered by a task scheduler. This console application performs the various actions and needed to be logged these. It would generate logs of around 200kb per run and the console app runs every hour.
Since the server is not accessible to us, I am planning to store the logs to Azure. I read about blob/table storage.
I would like to know what is the best strategy to store the logs in Azure.
Thank you.
Though you can write logging data in Azure Storage (both Blobs and Tables), it would actually make more sense if you use Azure Application Insights for logging this data.
I recently did the same for a console application I built. I found it incredibly simple.
I created an App Insight Resource in my Azure Subscription and got the instrumentation key. I then installed App Insights SDK and referenced appropriate namespaces in my project.
using Microsoft.ApplicationInsights;
using Microsoft.ApplicationInsights.DataContracts;
using Microsoft.ApplicationInsights.Extensibility;
This is how I initialized the telemetry client:
var appSettingsReader = new AppSettingsReader();
var appInsightsInstrumentationKey = (string)appSettingsReader.GetValue("AppInsights.InstrumentationKey", typeof(string));
TelemetryConfiguration configuration = TelemetryConfiguration.CreateDefault();
configuration.InstrumentationKey = appInsightsInstrumentationKey;
telemetryClient = new TelemetryClient(configuration);
telemetryClient.InstrumentationKey = appInsightsInstrumentationKey;
For logging trace data, I simply did the following:
TraceTelemetry telemetry = new TraceTelemetry(message, SeverityLevel.Verbose);
telemetryClient.TrackTrace(telemetry);
For logging error data, I simply did the following:
catch (Exception excep)
{
var message = string.Format("Error. {0}", excep.Message);
ExceptionTelemetry exceptionTelemetry = new ExceptionTelemetry(excep);
telemetryClient.TrackException(exceptionTelemetry);
telemetryClient.Flush();
Task.Delay(5000).Wait();//Wait for 5 seconds before terminating the application
}
Just keep one thing in mind though: Make sure you wait for some time (5 seconds is good enough) to flush the data before terminating the application.
If you're still keen on writing logs to Azure Storage, depending on the logging library you're using you will find suitable adapters that will write directly into Azure Storage.
For example, there's an NLog target for Azure Tables: https://github.com/harouny/NLog.Extensions.AzureTableStorage (though this project is not actively maintained).

Best way to log app data from a console app running inside a container on ACI

A seeming simple ask, but had no luck finding the best way.
So i need to log application events from a console app that will spin up inside a container and do some work then die.
How can i log custom data from inside?
I've tried Azure Monitor and created a workspace and used HTTP Data Collector API inside the app but no joy in working out where logs are being stored.
Is there a simple way to log to an Azure Storage account and then using Azure Monitor to manage the events?
I've been googling for hours but a lot of posts are 8 years old and not relevant and i cannot really find a simple use case in modern azure.
Perhaps it's so simple i just cannot see it
Any pointers or links greatly received!
thanks
Paul
Why not trace events using Application Insight custom events ?
https://learn.microsoft.com/en-us/azure/azure-monitor/app/api-custom-events-metrics
With that you can trace events with any metadata and check them in the Azure Application Insights Blade or reach them by the Application Insights SDK or The Api.
You just need to create an Application Insight instance and use the Telemetry Key to do that.
SDK: https://github.com/Microsoft/ApplicationInsights-dotnet
API : https://dev.applicationinsights.io/reference
Code sample to write events:
TelemetryClient client = new TelemetryClient();
client .InstrumentationKey = "INSERT YOUR KEY";
client.TrackEvent("SomethingInterestingHappened");
Also you can send more than just an string value:
tc.TrackEvent("PurchaseOrderSubmitted",
new Dictionary<string, string>()
{
{"CouponCode", "JULY2015" }
}, new Dictionary<string, double>()
{
{"OrderTotal", 68.99 },
{"ItemsOrdered", 5}
});

Get Instance Count of Azure Web App with C#

I've been looking around and I can't find a concise example around getting this metric. I've installed WindowsAzure.Management.Compute and now I don't know where to begin. How can this be done?
I would recommend you to take a look at Azure Resource Explorer: https://resources.azure.com. You could find the instances request URL as following:
For the Authorization header please have a look at this article. We can use C# HttpRequest to write the code. Here is the result I tested in fildder.
the endpoint:
https://management.azure.com/subscriptions/<subscription id>/resourceGroups/jatestgroup/providers/Microsoft.Web/sites/testcore1/instances?api-version=2015-08-01
Result:
We can calculate the instance number from the response json. In addition, it will need a long time in this rest API to show all instances when scale the instance in azure portal.
[Update]
According with my comment, I tested with the article:http://blog.amitapple.com/post/2014/03/access-specific-instance/#.V9tLKyh95hF
The following is my result:
Please download the library at here. Refer to this article for more information about Windows Azure Management Certificates.
Here is the code snipped:
var cert = new X509Certificate2();
cert.Import(Convert.FromBase64String(""));
var _client = new Microsoft.WindowsAzure.Management.WebSites.WebSiteManagementClient(new Microsoft.WindowsAzure.CertificateCloudCredentials("****", cert));
var ids= await _client.WebSites.GetInstanceIdsAsync("EastAsiawebspace", "testcore1");
You cannot get the instance count from within one instance of the Web App. But you can get it from the portal, or from the Azure ARM API (it's the numberOfWorkers property on the Web Hosting Plan object).
Also, note that WindowsAzure.Management.Compute does not apply to Azure Web App.

Alerts for exceptions in an Azure worker role

Is there an easy way to send an alert or notification in the Azure Management portal if a worker role throws an exception or has an error?
I am using Azure 2.5
I have tracing and diagnostics all set up and can view the logs in the server explorer of Visual studio, but is there anyway to set an alert if for example and error message appears in the logs.
I know you can set up alerts for monitoring metrics in the Management portal is there an easy way to add metrics for errors and exceptions?
Or someway to get C# exception code to create notifications or alerts in the Azure Management portal?
I ended up using email alerts and Application insights to monitor my worker role in the Azure portal. I created an Application insight on the portal according to these instructions.
Using the Nuget package manager in Visual Studio I added the Application insights API, Application insights for website (even though my worker role is not a web app) and the Application insights trace listener.
I then created an Application insight instance by adding the following to the worker role.
private TelemetryClient tc = new TelemetryClient();
And then adding this to the onStart method.
tc.Context.InstrumentationKey = "XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXX";
You can find your insturmentation Key in the Azure Portal.
After running, or deploying my worker role I could then view all of my Trace.TraceInformation and TraceError statements in the Azure portal as well as add tc.TrackError and tc.TrackEvent statements to track errors and events.
TrackError worked perfectly for notifying me when an exception was thrown.
I use SendGrid for sending emails through Azure because it's free. Here is what I would do something like below:
try
{
//....
}
catch(Exception ex)
{
MailMessage mailMsg = new MailMessage() {
//Set your properties here
};
// Add the alternate body to the message.
mailMsg.AlternateViews.Add(
AlternateView.CreateAlternateViewFromString(Body
, new System.Net.Mime.ContentType("text/html")));
SmtpClient smtpClient = new SmtpClient(
ServerGlobalVariables.SmtpServerHost
, Convert.ToInt32(587));
System.Net.NetworkCredential credentials =
new System.Net.NetworkCredential(
ServerGlobalVariables.SmtpServerUserName
, ServerGlobalVariables.SmtpServerPassword);
smtpClient.Credentials = credentials;
smtpClient.Send(mailMsg);
}
Please note that I store my credits in a globalvariables class called ServerGlobalVariables. Also, I send my emails formatted as HTML, but you don't have to do that.

Azure Storage services logs

I am a beginner in Azure and need some help. We are facing a bit of problem with Azure Storage services and are unable to proceed.
Ok now the issue is
http://blogs.msdn.com/b/windowsazurestorage/archive/2014/08/05/microsoft-azure-storage-service-version-removal.aspx
To summarize:
We have to inspect the log version of an/all of blobs,tables,queues in case any of them are using the one set for planned removal. I have enabled logging for the webapplication on the azure portal site. I am able to see the three services as under
https://.blob.core.windows.net
https://.table.core.windows.net
https://.queue.core.windows.net
Now in the articles as below I gather that we get the log format as this where they have a version included but have NOT specfied from where to locate the logs and how to gather the logs. I have tried different things from using https://.blob.core.windows.net/$logs but makes no difference.
The logs required should be in this format(sample)
Here is a sample log entry, with the version used highlighted – in this case the request was an anonymous GetBlob request which implicitly used the 2009-09-19 version:
1.0;2011-08-09T18:52:40.9241789Z;GetBlob;AnonymousSuccess;200;18;10;anonymous;;myaccount;blob;"https:// myaccount.blob.core.windows.net/thumbnails/lake.jpg?timeout=30000";"/myaccount/thumbnails/lake.jpg";a84aa705-8a85-48c5-b064-b43bd22979c3;0;123.100.2.10;2009-09-19;252;0;265;100;0;;;"0x8CE1B6EA95033D5";Friday, 09-Aug-11 18:52:40 GMT;;;;"8/9/2011 6:52:40 PM ba98eb12-700b-4d53-9230-33a3330571fc"
Can you please show me a way to view these logs. Any tool to use ?
Since these logs are stored in a blob container called $logs, any storage explorer which supports viewing data from this blob container can be used to view the contents. To the best of my knowledge following tools support viewing data from this container: Azure Storage Explorer, Cerebrata Azure Management Studio, Cloud Portam (Disclosure: I am the developer working on this tool).
However before you could view the data you would need to enable logging on your storage account. Only when logging is enabled on the storage account you will see this container show up in your storage account. To enable logging, again you can use Azure Management Studio or Cloud Portam or you could use the code below (the code I mentioned below assumes you have the latest version of Storage Client Library):
static void SetLoggingProperties()
{
CloudStorageAccount account = new CloudStorageAccount(new StorageCredentials(StorageAccount, StorageAccountKey), true);
LoggingProperties properties = new LoggingProperties()
{
LoggingOperations = LoggingOperations.All,
RetentionDays = 365,
Version = "1.0",
};
ServiceProperties serviceProperties = new ServiceProperties()
{
Cors = null,
HourMetrics = null,
MinuteMetrics = null,
Logging = properties,
};
var blobClient = account.CreateCloudBlobClient();
blobClient.SetServiceProperties(serviceProperties);
var tableClient = account.CreateCloudTableClient();
tableClient.SetServiceProperties(serviceProperties);
var queueClient = account.CreateCloudQueueClient();
queueClient.SetServiceProperties(serviceProperties);
}
Once logging properties are set, give it some time for logs to show up.

Resources