-
Notifications
You must be signed in to change notification settings - Fork 0
Using Azure Blob for Data File Output
If you want to store data files in external storage, Azure Blob can be a good and relatively cheap option. You can use the OpenDotNetDiagnostics.Counters.Sinks.AzureBlob NuGet package to output dotnet-counters
data to a designated Azure Blob storage. Here are the steps to use it:
First, decide how to authenticate to Azure Storage. Two approaches are supported today:
1. Connection string - Sets a connection string to the Azure Blob Storage. It's easy to use, but not as secure.
2. Passwordless - Uses `DefaultAzureCredential` for authorization. It's more secure but has a bit of a learning curve.
We will use connection string in this post for simplicity and later show how to use passwordless authorization. If you prefer to follow an example, check out here.
-
Configure authentication/authorization to write to Azure Blob
In the configuration file, for example, appsettings.json, add the connection string as shown below:
"DotNetCounterAzureBlobSink": { "ConnectionString": "Your-connection-string-for-Blob-Storage-Portal" }
Other common providers for configuration will work too. For example, you can put this configuration in the portal for deployed applications with the same effect:
DotNetCounterAzureBlobSink__ConnectionString = "Your-connection-string-for-Blob-Storage-Portal"
-
Add the NuGet package
- Assuming you have already exposed the endpoint for dotnet-counters, add the package
OpenDotNetDiagnostics.Counters.Sinks.AzureBlob
by running:
dotnet add package OpenDotNetDiagnostics.Counters.Sinks.AzureBlob --version 1.0.0-beta1
- Assuming you have already exposed the endpoint for dotnet-counters, add the package
-
Enable the sink as shown in the example code:
... builder.Services.AddDotNetCounterAzureBlobSink(); ...
-
Run your application.
When it succeeds, you will see
dotnet-counter
files show up in the Azure Storage:
-
We leverage
DefaultAzureCredential
to provide passwordless solution for authenticating/authorization. That means, any credential that is supported by theDefaultAzureCredential
is also supported by the Azure Blob sink. For more details, refer to Authenticate to Azure and authorize access to blob data. -
In our example code, we used the Azure CLI credentials. Here's a quick description of the steps:
-
Follow the steps to grant
Storage Blob Data Contributor
role to the credential you are going to use. In our case, it is our developer account. -
Use a configuration like this:
"DotNetCounterAzureBlobSink": { "ServiceUri": "https://yourtargetblob.blob.core.windows.net/" }
-
Login locally using Azure CLI:
az login
-
Run the application.
-
-
It is also very common to use Managed Identity for the deployed application. It is almost the same except:
-
The service principal in the role assignment step above needs to go to the managed identity.
-
For User-Assigned managed identity, you will need to specify the managed identity client id:
"DotNetCounterAzureBlobSink": { "ServiceUri": "https://yourtargetblob.blob.core.windows.net/", "ManagedIdentityClientId": "the-managed-identity-client-id" }
-
Do NOT forget the assign the identity to the application you deployed.
-