Read ApplicationInsights trace events with Insights library - c#

Writing trace events to Applications Insights is extremely easy on any platform. For example, in C# under dotnet core it is:
Client.InstrumentationKey = InstrumentationKey;
Client.TrackTrace("Test Trace from DotNet Console App.");
But reading that data back appears to have no such simple API, at least via NuGet.
I have seen the documentation for Kusto:
https://learn.microsoft.com/en-gb/azure/kusto/api/netfx/about-kusto-ingest
But the closest I've come to simply and easily reading trace events is by reading the documentation for the API Explorer and converting that into dotnet core C#:
using (var client = new HttpClient(new HttpClientHandler {}))
{
client.DefaultRequestHeaders.Add("x-api-key", ApiKey);
var response = client.GetAsync(InsightsUrl).Result;
var succ = response.IsSuccessStatusCode;
var body = response.Content.ReadAsStringAsync().Result;
var path = $#"{AppDomain.CurrentDomain.BaseDirectory}..\..\..\Insights.json";
File.WriteAllText(path, body);
}
What is a comparably easy method to use for reading Insights trace (etc) events without having to build a web client?

Actually, no other simple ways like 1 or 2 line method for reading the trace(and other telemetry data) back.
As of now, the web api you used is the best way to achieve that.

Related

simple webjob - process the response from a web link and save it to blob on a regular time interval - imposible to find an example or solution

I am looking for an example for a simple webjob:
the task would be to process the response from a web link and save it to blob on a regular time interval.
first of all the ms documentation is confusing me as far as time triggers are concerned:
https://learn.microsoft.com/en-us/azure/app-service/webjobs-create#ncrontab-expressions
https://learn.microsoft.com/en-us/azure/azure-functions/functions-bindings-timer?tabs=csharp#example
and also how exactly should I proceed on building the WebJob, should I use an azure webjob template (.net 4.x.x), or .net core console app ??
https://learn.microsoft.com/en-us/azure/app-service/webjobs-sdk-how-to
https://github.com/Azure/azure-webjobs-sdk-samples/tree/master/BasicSamples
https://learn.microsoft.com/en-us/azure/app-service/webjobs-sdk-get-started
https://learn.microsoft.com/en-us/azure/app-service/webjobs-create
all this resource and no simple example for a time scheduled task that would get a web response, also the confusion on building the webjob VS, wth?? I want to build a c# app in VS and deploy to azure as webjob via azure devops.
wasted 3 days on this since im not a .net developer...
Webjobs have changed and grown over the years including contributions from Azure Functions, which is also built on top of the Webjobs SDK. I can see how this can get confusing, but the short answer is that all of the different methods are still valid, but some are newer than others. Of the two timer trigger styles, the second is more current.
I generally recommend Functions instead of Webjobs for something like this since at this point as it will save you some boiler-plate code, but it is entirely up to you. As I mentioned, the foundations are very similar. You can deploy Functions apps to any App Service plan, including the Consumption plan- this is specific to Functions that is pay-by-usage instead of a monthly fee like you would need for WebJobs.
As far as .NET Framework vs. .NET Core, you can use it will depend on what runtime you used to set up your App Service. If you have a choice, I would recommend using Core since that will be the only version moving forward. If you elect to use Functions, you will definitely want to use Core.
As far as the Console App question, all WebJobs are essentially console apps. From a code perspective, they are a console app that implements the Webjobs SDK. You could run them outside of Azure if you wanted to. Functions apps are different. The Function's host is what actually runs behind the scenes and you are creating a class library that the host consumes.
Visual Studio vs. Visual Studio Code is very much a personal preference. I prefer VS for Webjobs and work with both VS and VS Code for Functions apps depending on which language I am working in.
The most basic version of a Webjob in .NET Core that pulls data from a webpage on a schedule and outputs it to blob storage would look something like this. A Function app would use exactly the same GetWebsiteData() method plus a [FunctionName("GetWebsiteData")] at the beginning, but you wouldn't need the Main method as that part is handled by the host process.
public class Program
{
static async Task Main(string[] args)
{
var builder = new HostBuilder();
builder.ConfigureWebJobs(b =>
{
b.AddAzureStorageCoreServices();
b.AddAzureStorage();
b.AddTimers();
});
builder.ConfigureAppConfiguration((context, configurationBuilder) =>
{
configurationBuilder
.AddJsonFile($"appsettings.json", optional: true);
});
var host = builder.Build();
using (host)
{
await host.RunAsync();
}
}
public async static void GetWebsiteData(
[TimerTrigger("0 */1 * * * *")] TimerInfo timerInfo,
[Blob("data/websiteData", FileAccess.Write)] Stream outputBlob,
ILogger logger)
{
using(var client = new HttpClient())
{
var url = "https://microsoft.com";
var result = await client.GetAsync(url);
//you may need to do some additional work here to get the output format you want
outputBlob = await result.Content.ReadAsStreamAsync();
}
}
}

How to resolve exception **System.MissingMethodException:** 'Method not found: void Amazon.S3.Transfer.TransferUtility.Upload()' (Xamarin AWS S3)

I'm going through the tutorials by AWS Mobile SDK - Xamarin Developer Guide and I want to upload a text file from my Android phone (through Xamarin) to AWS S3. These are the 2 tutorials: Setting Up the AWS Mobile SDK for .NET and Xamarin and Store and Retrieve Files with Amazon S3.
I'm using the method void Amazon.S3.Transfer.TransferUtility.Upload(string,string,string) to upload my text file. I tried running my code but I couldn't. I run into this exception **System.MissingMethodException:** 'Method not found: void Amazon.S3.Transfer.TransferUtility.Upload(string,string,string)' despite the fact that I have included the correct namespaces. I have confirmed that the method void Amazon.S3.Transfer.TransferUtility.Upload(string,string,string) is found in the namespace Amazon.S3.Transfer. Below is my code
client = new AmazonS3Client(credentials, bucketRegion);
var fileTransferUtility = new TransferUtility(client);
fileTransferUtility.Upload(_fileName, bucketName, "toDo.txt");
I've included the correct namespaces, such as using Amazon.S3; using Amazon.S3.Transfer;
The above code works for Windows Presentation Foundation (WPF) Application, but not on Xamarin.
I've add the latest version of
AWSSDK.Core (v3.3.107.8)
AWSSDK.S3 (v3.3.111.9)
How do I resolve this error?
Any help is highly appreciated. Thank you so much in advance.
I have successfully uploaded my text file. Many thanks to #AniketBhansali for suggesting to check for internal exception, and giving UploadAsync a try. Let me detail my debugging process.
In summary, to upload my text file, I
Checked for internal exception
Did UploadAsync
Changed my credentials from the one Amazon Cognito credentials provider gave, to the IAM user I'm using to access my AWS account
For some reason, transferUtility.Upload() is not working, but transferUtility.UploadAsync() is.
So at first I checked for S3 specific exception like this
try
{
var fileTransferUtility = new TransferUtility(client);
fileTransferUtility.Upload(_fileName, bucketName, "toDo.txt");
}
catch(AmazonS3Exception exception)
{
Console.WriteLine("Error encountered on server. Message:'{0}' when writing an object", exception.Message);
}
catch (Exception exception)
{
Console.WriteLine("Unknown encountered on server. Message:'{0}' when writing an object", exception.Message);
}
However, the same exception message still occured. So instead of Upload I tried UploadAsync like this
await fileTransferUtility.UploadAsync(_fileName, bucketName, "toDo.txt");
And thankfully, I got a new error message. Unknown encountered on server. Message:'Object reference not set to an instance of an object' when writing an object
I Google Searched and found this Null reference exception with AWS S3 API.
From this question thread, I suspected that the error occured because I was using the credentials from Amazon Cognito credentials provider.
Therefore, I changed the credentials to the IAM user I'm using to access my AWS account and I managed to send my text file to AWS S3.
AWS evolves pretty quickly, so it is possible that they stopped supporting the older methods in their newer SDKs. The latest guide from AWS recommends using transferUtility.Upload(file, bucketName) instead. This is what it should look like:
// Initialize the AWS TransferUtility client
var s3Client = new AmazonS3Client(credentials,region);
var transferUtility = new TransferUtility(s3Client);
// Upload on the Transfer Utility object
transferUtility.Upload(
Path.Combine(Environment.SpecialFolder.ApplicationData,"file"),
"bucketName"
);

Tracking outgoing requests in Azure Functions

As part of a Microservice based solution that we are building we have a number of Azure Functions sitting in one Azure Function App. The functions Orchestrate numerous requests to different APIs some of which take a long time to complete. We added Application Insights to the functions to allow for some tracking of the requests made, but dependency tracking is not working yet in Azure Functions. It is possible to manually track dependencies but that involves inserting some tracking code around each dependency call, however we want to avoid manually tracking dependencies on each and every call.
One of the solutions I have thought of would be to create a request tracker that tracks all outgoing web requests from the functions. Within the request tracker I could then track the dependency requests including their time. I want to hook the request tracker into some sort of web traffic handler, unfortunately I was unable to find much about doing this. A lot of posts mention using System.Net trace writer for this, but as far as I can see this requires a Web.config to setup and functions do not have one.
I have seen a few posts mentioning to create a request wrapper and place that on my outgoing requests, but unfortantely that is not an option as we use a number of packages that make requests internally. If you have any ideas that could get me going in the right direction please let me know. Thanks
Update:
I added the following helper method which allows me to manually track tasks as dependency requests
public static async Task<T> TrackDependency<T>(this Task<T> task, string dependecyName, string callName, string operationId)
{
var telemtryClient = new TelemetryClient();
var startTime = DateTime.UtcNow;
var timer = System.Diagnostics.Stopwatch.StartNew();
var success = true;
T result = default(T);
try
{
result = await task;
}
catch (Exception)
{
success = false;
}
finally
{
timer.Stop();
var dependencyTelemetry = new DependencyTelemetry(dependecyName, callName, startTime, timer.Elapsed, success);
dependencyTelemetry.Context.Operation.Id = operationId;
telemtryClient.Track(dependencyTelemetry);
}
return result;
}
It can then be used as follows:
client.Accounts.UpdateWithHttpMessagesAsync(accountId, account).TrackDependency("Accounts", "UpdateAccounts", requestContextProvider.CorrelationId);
I can now see individual request dependencies in Application Insights, but obviously the actual telemetry on them is very limited, it does not contain path info or much else.
So when you say dependency tracking is not working in Azure Functions, what exactly do you mean? Have you actually added and configured the Application Insights SDK to your actual function yet? The out-of-the-box monitoring experience with Azure Functions doesn't automatically add dependency tracing, but if you actually add/configure the Application Insights SDK in your function project it should start tracking everything going on in there.

How to deploy an app to azure using ARM and C#?

I've seen some examples of how to create storage resources using ARM and C#. I'm assuming that the same is possible for apps. However, I can't find a working example.
Ideally I'd like to have a c#/Web api app that would be able to deploy another app. The process should be fully automated.
Basically, I'd like to create a new instance of the same app with its own configuration - the process would be triggered by a new customer signing up for my SaaS.
Could someone please give some pointers on how to deal with the above?
Thanks.
It seems that you’d like to deploy web application (deployment package) to Azure app service web app programmatically in C# code, you can try to use Kudu Zip API that allows expanding zip files into folders. And the following sample code works fine for me, you can refer to it.
//get username and password from publish profile on Azure portal
var username = "xxxxx";
var password = "xxxxxxxxxxxxxxxxxxxx";
var AppName = "{your_app_name}";
var base64Auth = Convert.ToBase64String(Encoding.Default.GetBytes($"{username}:{password}"));
var file = File.ReadAllBytes(#"C:\Users\xxx\xxx\WebApplication1.zip");
MemoryStream stream = new MemoryStream(file);
using (var client = new HttpClient())
{
client.DefaultRequestHeaders.Add("Authorization", "Basic " + base64Auth);
var baseUrl = new Uri($"https://{AppName}.scm.azurewebsites.net/");
var requestURl = baseUrl + "api/zip/site/wwwroot";
var httpContent = new StreamContent(stream);
var response = client.PutAsync(requestURl, httpContent).Result;
}
Besides, you can use Microsoft.Azure.Management.Fluent to manage your Azure app service.
You could grab the Azure Management Libraries for .Net from Github. Support for Azure App Service is in preview as of v1.2. This enables a very intuitive syntax.
var webApp = azure.WebApps.Define(appName)
.WithRegion(Region.USWest)
.WithNewResourceGroup(rgName)
.WithNewFreeAppServicePlan()
.Create();
There are lots of code samples for both Windows and Linux flavours of Azure App Service shown here: https://github.com/Azure/azure-sdk-for-net/tree/Fluent
There are also some worthwhile reading materials on how to handle data in this kind of scenario; various possibilities and design patterns are covered at https://learn.microsoft.com/en-us/azure/sql-database/sql-database-design-patterns-multi-tenancy-saas-applications

Best way to create python web service and c# client application

I am looking for a simple and reliable way to create Python Web Service and consume it from the .Net (c#) application.
I found plenty of different libraries, where one is better than another, but nobody seems to have a complete working example with Python Web Service and some simple c# client. And reasonable explanations of steps to configure and run
I am suggesting using Tornado. It is very simple to use, non-blocking web-server written in Python. I've been using it in the past and I was shocked how easy it was to learn and use it.
I am strongly encouraging you to design your API with REST in mind. It will make your API simple, and easy to consume by any language/platform available.
Please, have a look at the 'Hello World' sample - it has been taken from Torando's main site:
import tornado.ioloop
import tornado.web
class MainHandler(tornado.web.RequestHandler):
def get(self):
self.write("Hello, world")
application = tornado.web.Application([
(r"/", MainHandler),
])
if __name__ == "__main__":
application.listen(8888)
tornado.ioloop.IOLoop.instance().start()
As for the client part - there is nothing complicated:
string CreateHTTGetRequest(string url, string cookie)
{
WebRequest request = WebRequest.Create(url);
request.Method = "GET";
request.Headers.Add("Cookie", cookie);
WebResponse response = request.GetResponse();
Stream stream = response.GetResponseStream();
StreamReader reader = new StreamReader(stream);
string content = reader.ReadToEnd();
reader.Close();
response.Close();
return content;
}
In case the server is running on your local machine, the URI is: 'http://localhost:8888/'
you may start your practice by:
Install ZSI
Create a WSDL for your service
The full example
4.On client(C#) follow this tutorial

Categories

Resources