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,
- #Function to write trace information
- functionWrite-AzureTrace
- {
- param
- (
- [System.String]$logMessage,
-
- [ValidateSet('Show','None')]
- [System.String]$consoleOutput
- )
-
- [System.Diagnostics.Trace]::WriteLine($logMessage)
-
- }
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.
- #Importing custom trace listener dll
- Add-Type-Path"D:\Scripts\Demo.dll"
-
- #Initializing required attrbutes of trace listener
- $customListener=new-object"Demo.BlobWriterStorageListener"
- $customListener.Attributes.Add("StorageConnectionString","Your_storage_conn_string")
- $customListener.Attributes.Add("LogsContainerName","logs")
- $customListener.Attributes.Add("LogFileName","application.log")
-
- #Adding custom trace listener to collection
- [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.
- 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.
- #Importing custom trace listener dll
- Add-Type-Path"D:\Scripts\Demo.dll"
-
- #Initializing required attrbutes of trace listener
- $customListener=new-object"Demo.BlobWriterStorageListener"
- $customListener.Attributes.Add("StorageConnectionString","Your_storage_conn_string")
- $customListener.Attributes.Add("LogsContainerName","logs")
- $customListener.Attributes.Add("LogFileName","application.log")
-
- #Adding custom trace listener to collection
- [System.Diagnostics.Trace]::Listeners.Add($customListener)
-
- #Function to write trace information
- FunctionWrite-AzureTrace
- {
- param
- (
- [System.String]$logMessage,
-
- [ValidateSet('Show','None')]
- [System.String]$consoleOutput
- )
-
- [System.Diagnostics.Trace]::WriteLine($logMessage)
-
- }
-
- #Writing Trace
- Write-AzureTrace-logMessage"Hey There! This is from PowerShell!"-consoleOutputShow
Feel free to post your comments or views.