Showing posts with label Microsoft Azure. Show all posts
Showing posts with label Microsoft Azure. Show all posts

Thursday, 22 January 2026

Store Application Insights in Disk and Send it to Azure Monitor when Button Clicks in Asp.Net Core

In this post we are going to see how to store the application insights in Disk and send it to azure monitor when button click, both this two operation must be in same AppDomain or AppContext. 

Configure the ITelemetryChannel as store the logs in Disk path, we will store that in AppData Local Storage. The folder what we given here must be already created inside AppData. Then only the logs will be created.

Install the following package Microsoft.ApplicationInsights.AspNetCore in Asp.Net core Project. Then add the below lines in program.cs


          Inject (ITelemetryChannel channel)
          this.channel.Flush();

From this article you can learn how to save the application insights locally in Disk and send it later when you required to Azure Monitor.

Tuesday, 20 January 2026

How to use Application Insights in ASP.Net Core

In this tutorial we are going to see how to use the Application Insights in asp.net core. and also explain about how to use some features of application insights. First we see few steps for basic level of logging, then we will see other features present in application insights.

Additional Features:

  1. Use Custom Properties
  2. Log Different messages
  3. Controller Level Configuration
  4. Remove or Filter Modules

Steps for Basic Level of Logging:
First we have to install Microsoft.ApplicationInsights.AspNetCore in Asp.Net Core Project. Configure program.cs like below to use application insights.

           builder.Services.AddApplicationInsightsTelemetry();

The Default LogLevel for ApplicationInsightsLoggerProvider is Warning.  if you try to Log Debug or Trace it won't Log, because default level is Warning.
 
Setting Log Level of Default Provider (ApplicationInsightsLoggerProvider) to Debug
will get change based on the Environment you deploy your application, so load the
LogLevel from configuration and set to Default Provider (ApplicationInsightsLoggerProvider).


Before that we have know that LogLevel will work in Bottom to Top order. If you give value of enum Critical as LogLevel, then only Critical will log. if you give LogLevel value as Warning, then Logs from "Critical", "Error" and "Warning" will Log. it will log from Bottom to Top approach until which LogLevel will you give. So the Default Value now is Warning, to log Debug, we have to remove the default Rule and create one. When you give Empty value in AddFilter for first param then it will take default provider with log level what we mention in second parameter.




Above code will make default log level to Debug, so now up to Log Debug will work. Now you can log the messages in your code, Make sure you are getting the log level from configuration File. Now we see different features present in Application Insights, above 2 steps is enough to log messages up to Debug Level, if you don't want Log Debug, then change that to higher level like LogLevel.Error for AddFilter of ApplicationInsightsLoggerProvider.


1. Use Custom Properties
we will take a example of custom TelemetryProcesssor where custom properties for each HttpRequest will be logged.


2. Log Different Messages
We have to inject ILogger<ControllerName> then use the object and call the methods inside that. Here we are using two methods LogDebug and LogError.



3. Controller Level Configuration
How to set the LogLevel configuration at controller level, we have to do the below config in program.cs. You can see that AuthorController logs only Error, even though if you have LogDebug method  in code, but in insights it wont get logged.


4. Remove or Filter Modules
In Module level we can filter the messages like in Dependencies are logged in application insights, if we don't want SQL Text from EF core wont need to log then configure EnableSqlCommandTextInstrumentation item as "false". 



if you don't want the Dependencies are not allow to log then we have to remove the module like below in Program.cs.



Before Remove Module: you can see Dependency, After Remove Module: you can see Dependency(0). From this tutorial you can learn how to use the Application Insights in Asp.Net Core.

Monday, 22 April 2019

Timer trigger in web job Azure

In this post we are going to see how to create the timer trigger in azure and what is the purpose of it.

TimerTrigger is the attribute which does the trigger for execute a function at certain intervals in webjob.

Code:
class Program
  {
    static void Main(string[] args)
    {
      JobHostConfiguration config = new JobHostConfiguration();
      config.NameResolver = new TriggerResolver();
      config.UseTimers();

      JobHost host = new JobHost(config);
      host.RunAndBlock();
    }

    private class TriggerResolver: INameResolver
    {
      public string Resolve(string name)
      {
        string value = ConfigurationManager.AppSettings[name];              
      }
    }
  }
  
  public class Startup
  {
    public static void ScheduleTrigger([TimerTrigger("%Schedule%")] TimerInfo timer)
    {
      
    }
  }
}



From the above code you can see the schedule information is loaded from configuration file using INameResolver interface

Configuration value:
<add key=”Schedule” value=”24:00:00″/>


in another way we can schedule the timer using the typeof a class or cron expression

public static void LoggingFunction([TimerTrigger("0 0 6 * * *", RunOnStartup = false)] 
TimerInfo timerInfo, TextWriter log) 
{

  //Do stuff every day at 6AM

}


in the above cron expression

*    *    *    *    *    *  command to be executed
{second} {minute} {hour} {day} {month} {day-of-week}


From this post you can learn Timer Trigger in Azure webjobs

Sunday, 16 September 2018

Delete a Queue in Microsoft Azure storage account.

In this post we are going to see how to delete a queue in Microsoft Azure storage account.

Install the Following package
1. WindowsAzure.Storage
2. Install-Package Microsoft.WindowsAzure.ConfigurationManager -Version 3.2.3

class Program
    {

        // Nuget Packages
        // -Install-Package Microsoft.WindowsAzure.ConfigurationManager -Version 3.2.3
        static void Main(string[] args)
        {
            CloudStorageAccount account = CloudStorageAccount.Parse(CloudConfigurationManager.GetSetting("azureStorageAccount"));
            var queueClient = account.CreateCloudQueueClient();

            /* Get the reference of the queue */
            var testQueue = queueClient.GetQueueReference("testingqueue");

            /* Deleting the queue if exists */
            testQueue.DeleteIfExists();

        }

    }

From the above code you can learn how to delete a queue in Microsoft Azure storage account.

Create a Queue in Microsoft Azure storage account and send message to it

In this post we are going to see how to create a queue in Microsoft Azure storage account and send the message to that queue. First we have to configure the connectionstring of the storage account in the appsetting of app.config.

Install the Following package
1. WindowsAzure.Storage
2. Install-Package Microsoft.WindowsAzure.ConfigurationManager -Version 3.2.3

Queue name should be lower case otherwise it will return bad request.

Please see this link for Queue naming rules: https://msdn.microsoft.com/en-us/library/azure/dd179349.aspx.

After installing above two packages now start the coding.

class Program
    {

        static void Main(string[] args)
        {
            CloudStorageAccount account = CloudStorageAccount.Parse(CloudConfigurationManager.GetSetting("azureStorageAccount"));

            var queueClient = account.CreateCloudQueueClient();

            /* Get the reference of the queue */
            var testQueue = queueClient.GetQueueReference("testingqueue");

            /* Create a Queue if not exists */
            testQueue.CreateIfNotExists();

            CloudQueueMessage message = new CloudQueueMessage("Hai sample");

            /* Adding a message to Queue */
            testQueue.AddMessage(message);


        }

    }

use the Microsoft Azure storage Explorer for viewing the queue message.
https://azure.microsoft.com/en-us/features/storage-explorer/


Output:
************





From this post you can learn how to create a queue in Microsoft Azure storage account and send the message to that queue.

Saturday, 15 September 2018

Create Azure Durable Functions which includes Orchestrator Function and Activity Function - Part 2

In this post we are going to see the implementation of  Durable Function which includes Orchestrtor Function and Activity Function

Please click on the below link for the Azure Durable Function source code
AzureDurableFunctions Source Code


We will take a scenario, inside the Orchsetrator Function, we will call two Activity Function, one Activity function returns the books list and second activity will save the books list to the Azure Table Storage.

1. Create a New Project
2. Select cloud in the left pane
3. Select Azure Functions in the right pane.
4. Give Azure Function name as "BookFunction" and click ok.



5. Select the HttpTrigger from the menu
6. Click ok




7. Install the following nuget package in the solution

Microsoft.Azure.WebJobs.Extensions.DurableTask




8. Change the input parameter for the function. add the OrchestrationClient Attribute with     
    DurableOrchestrationClient

[OrchestrationClient]DurableOrchestrationClient starter,



9. TestFunction.cs
******************


public static class TestFunction
    {
        [FunctionName("TestFunction")]
        public static async Task<HttpResponseMessage> Run(
            [HttpTrigger(AuthorizationLevel.Function, "get", "post",
                         Route = null)]HttpRequestMessage req,
            [OrchestrationClient]DurableOrchestrationClient starter,
            TraceWriter log)
        {
            log.Info("C# HTTP trigger function processed a request.");
            // Function input comes from the request content.
            string instanceId = await starter.StartNewAsync("Orchestrator", "TedTalk");

            log.Info($"Started orchestration with ID = '{instanceId}'.");
               
            return starter.CreateCheckStatusResponse(req, instanceId);
        }       
        

    }


Let we start the Program for Azure Durable Functions. From the above code you can see that we are calling a durable function Orchestrator  await starter.StartNewAsync("Orchestrator""TedTalk");


10. Add another Function named it as OrchestratorFunction , It is decorated by [OrchestrationTrigger] with DurableOrchestrationContext, Now if you see inside the function we are calling two Activity function named "GetAllData" and "SaveData"

OrchestratorFunction.cs
*********************************


    public class OrchestratorFunction
    {
        [FunctionName("Orchestrator")]
        public static async Task<string> RunOrchestrator([OrchestrationTrigger]
                 DurableOrchestrationContext context)
        {
            var name = context.GetInput<string>();

            // retrieves the list of data by invoking a separate Activity Function.
            var books = await context.CallActivityAsync<List<Book>>("GetAllData", name);
            if (books.Count > 0)
            {
                //Saving the retrieved data to table
                await context.CallActivityAsync("SaveData", books);
            }
            return context.InstanceId;
        }

    }



11. Add another Function named it as ActivityFunction, It is decorated by [ActivityTrigger] with DurableActivityContext, Now create two functions inside the file one is for "GetAllData" another one is for "SaveData", This two activity functions are called inside orchestration function.

GetAllData: This activity function will return the list of books
SaveData: This activity function will save the books list to Azure Table Storage.




ActivityFunction.cs
******************************


public static class ActivityFunction
    {
        private static CloudStorageAccount account = CloudStorageAccount.Parse(Environment.GetEnvironmentVariable("AzureWebJobsStorage",                                    EnvironmentVariableTarget.Process));

        
        [FunctionName("GetAllData")]
        public static async Task<List<Book>> GetAllData( 
                            [ActivityTrigger]DurableActivityContext context)
        {
            // retrieves the book name from the Orchestrator function
            var organizationName = context.GetInput<string>();

            return new List<Book> { new Book{ Id = 1, Name = "C#" }, 
                                    new Book{ Id = 2, Name = "Java" } };
        }



        [FunctionName("SaveData")]
        public static async Task SaveData([ActivityTrigger]DurableActivityContext context)
        {
            // retrieves a list of books from the Orchestrator function
            var books = context.GetInput<List<Book>>();

            // create a table storage client
            var client = account.CreateCloudTableClient();
            var table = client.GetTableReference("Books");

            await table.CreateIfNotExistsAsync();

            TableBatchOperation tableBatchOperations = new TableBatchOperation();

            for(int i=0; i<books.Count; i++)
            {
                tableBatchOperations.Add(TableOperation.InsertOrMerge(
                    new BookRepository(books[i].Id)
                    {
                        Name = books[i].Name
                    }));
            }

            await table.ExecuteBatchAsync(tableBatchOperations);

        }
    }

    public class BookRepository: TableEntity
    {
        public BookRepository(int id)
        {
            PartitionKey = "TechnicalBooks";
            RowKey = id.ToString();
        }

        public string Name { set; get; }
    }

    public class Book
    {
        public int Id { get; set; }

        public string Name { get; set; }


    }


Now change values in the local.settins.json for AzureWebJobsStorage to connect the azure table storage.

{
    "IsEncrypted": false,
  "Values": {
    "AzureWebJobsStorage": "UseDevelopmentStorage=true",
    "AzureWebJobsDashboard": "UseDevelopmentStorage=true"
  }

}



12. Now run the application, it will results in display the window like below




13. In the console Windows you can see the http url to hit the function, copy that url and paste that in browser. Now Orchestrator Function will be hit and two Activity Functions will be executed.




14. You see the Azure table storage , you can see the data populated from activity function.




Full source code:
*******************
Please click on the below link for the Azure Durable Function source code

AzureDurableFunctions Source Code


From this post you can learn how to use the Azure Durable Functions which includes Orchestrator Function and Activity Function.