Integrating Custom Azure Trace Listener With PowerShell

Before continuing with this post, it is recommended that you should check this post where we covered the basics of .NET Tracing, Trace Listener concepts and also built a custom trace listener to write all tracing information in Azure blob storage.

In this article, we will focus on how we can integrate the custom trace listener in PowerShell.

As we know that Windows PowerShell is an automation and configuration management framework built on top of .NET framework, this means that you can easily work with all managed .NET providers in PowerShell too. You can take a look at this post for your reference where we directly consumed Azure application insights API in PowerShell.

So let’s see how we can leverage .NET tracing in PowerShell.

In previous article, we already created a class library in which we had our custom trace listener which dumps all the traced information to azure blob storage, let’s grab the output of the class library and paste in a folder where we will be creating our PowerShell script file.

Let’s create a new PowerShell script by launching Windows PowerShell ISE (you can do it using notepad file too if you are a pro PowerShell developer unlike me).

Let’s create a common function which will be responsible for writing trace information,
  1. #Function to write trace information  
  2. functionWrite-AzureTrace  
  3.  {  
  4. param  
  5.     (  
  6.         [System.String]$logMessage,  
  7.   
  8. [ValidateSet('Show','None')]  
  9.         [System.String]$consoleOutput  
  10.                )  
  11.   
  12. [System.Diagnostics.Trace]::WriteLine($logMessage)  
  13.   
  14.  }   
As shown in the example above, the Write-AzureTrace function has two parameters i.e. logMessage and consoleOutput. The log message parameter is the message which we need to write in trace and consoleOutput parameter is to decide whether we want to display traced message on PowerShell console or not.

Once we are done with creating a function, let’s make PowerShell aware that it has to use the custom trace listener which writes this trace information to blob file in Azure storage.

It is quite similar to the process of adding trace listener to any .NET based application programmatically. I.e. adding our custom trace listeners in existing listeners’ collection.
 
Add this at the top of PowerShell script file.
  1. #Importing custom trace listener dll  
  2. Add-Type-Path"D:\Scripts\Demo.dll"  
  3.  
  4. #Initializing required attrbutes of trace listener   
  5. $customListener=new-object"Demo.BlobWriterStorageListener"  
  6. $customListener.Attributes.Add("StorageConnectionString","Your_storage_conn_string")  
  7. $customListener.Attributes.Add("LogsContainerName","logs")  
  8. $customListener.Attributes.Add("LogFileName","application.log")  
  9.  
  10. #Adding custom trace listener to collection   
  11. [System.Diagnostics.Trace]::Listeners.Add($customListener)    
As you can observe, we are simply importing the reference to the dll in PowerShell session and creating new object of our Custom trace listener. Then we are initializing all the required attributes of our trace listener by adding new objects in Attributes collection.

As a last step, we are registering object of custom trace listener with existing trace listeners’ collection.

By doing this we are all done with setting required configurations, so let’s go ahead and use the function.
  1. Write-AzureTrace-logMessage"Hey There! This is from PowerShell!"-consoleOutputShow   
And let’s validate if it has logged this information in a blob file in Azure storage. (You can either use Azure storage explorer in Visual Studio or other tools like Microsoft Azure Storage Explorer to browse through your storage entities).



Let’s open up this file and see the logged entry.



Isn’t it cool?

Below is the entire source.
  1. #Importing custom trace listener dll  
  2. Add-Type-Path"D:\Scripts\Demo.dll"  
  3.  
  4. #Initializing required attrbutes of trace listener   
  5. $customListener=new-object"Demo.BlobWriterStorageListener"  
  6. $customListener.Attributes.Add("StorageConnectionString","Your_storage_conn_string")  
  7. $customListener.Attributes.Add("LogsContainerName","logs")  
  8. $customListener.Attributes.Add("LogFileName","application.log")  
  9.  
  10. #Adding custom trace listener to collection   
  11. [System.Diagnostics.Trace]::Listeners.Add($customListener)    
  12.  
  13. #Function to write trace information  
  14. FunctionWrite-AzureTrace  
  15.  {  
  16. param  
  17.     (  
  18.         [System.String]$logMessage,  
  19.   
  20. [ValidateSet('Show','None')]  
  21.         [System.String]$consoleOutput  
  22.                )  
  23.   
  24. [System.Diagnostics.Trace]::WriteLine($logMessage)  
  25.   
  26.  }   
  27.  
  28. #Writing Trace  
  29. Write-AzureTrace-logMessage"Hey There! This is from PowerShell!"-consoleOutputShow   
Feel free to post your comments or views. 

Up Next
    Ebook Download
    View all
    Learn
    View all