Entity Framework Core in .NET is highly effective at automatically generating and executing queries behind the scenes. However, there may be instances where it is beneficial to view the auto-generated queries. This could be for purposes such as verifying the accuracy of the query or troubleshooting performance-related issues.
To achieve this, you can easily enable the display of the auto-generated SQL queries on the application console.
To configure console logging, you should include the OnConfiguring method within the database context class as shown below.
public class ApplicationDbContext : DbContext
{
public ApplicationDbContext(DbContextOptions options)
: base(options)
{
}
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
=> optionsBuilder.LogTo(Console.WriteLine);
}
Important: This code should be used for development and debugging purposes only. It should never be used in a production environment.
Hope this was helpful. Feel free to give feedback in the comments. Thank you!
The most common way to browse or download a storage blob is to go directly to the Azure portal. However, when there is a need for bulk downloads or automation, one may require writing custom scripts.
This post covers:
Getting a list of blobs in a storage account container
Applying filters/criteria to narrow down the list
Downloading the blobs in the list in parallel
You can also watch deep explanation on YouTube
1. Install azure PowerShell module.
Command to check Az module is already installed
Get-Module -Name Az -ListAvailable
If Az module is already installed, use below command to update it
Update-Module -Name Az -Force
If Az module is not installed, use below command to install it
Install-Module -Name Az -Repository PSGallery -Force
2. Create a new powershell script file, DownloadBlobs.ps1 and add below code to create DownloadBlobs function.
# Function to download the blobs function
DownloadBlobs() {
param(
[Parameter(Mandatory=$true)][Object]$storageContext, [Parameter(Mandatory=$true)][string]$containerName, [Parameter(Mandatory=$true)][string]$downloadLocation, [Parameter(Mandatory=$true)][Int32][ValidateScript({$_ -lt 0})] [int]$lastModifedInDays
)
$throttleLimit = 4
# Get the list of blobs
$blobList = Get-AzStorageBlob -Container $containerName -Context $storageContext
Write-Output "Total blobs in the container: " $blobList.Count
# Select blobs that has been modifed in last $lastModifedInDays days
$filteredBlobs = $blobList | Where-Object { $_.LastModified -gt ((Get-Date).Date).AddDays($days) }
Write-Output "Total blobs matching criteria: " $filteredBlobs.Count
Write-Output "Downloading...."
$filteredBlobs | ForEach-Object -Parallel { Get-AzStorageBlobContent -Container $using:containerName -Blob $_.Name -Context $using:storageContext -Destination $using:downloadLocation } -ThrottleLimit $throttleLimit
Write-Output "Download Complete" }
The function accepts four parameters:
storageContext: An object containing the details for connecting to the storage account.
containerName: The name of the container where the blobs are stored.
downloadLocation: The location on your machine where the blobs will be downloaded.
lastModifiedInDays: A filter that allows you to download blobs created within the last lastModifiedInDays from today.
The function first retrieves a list of all blobs in the container and then applies the days filter. You can choose to combine both steps or skip the filter, depending on your needs.
It then downloads the blobs using parallel iterations. The throttleLimit variable controls the number of parallel threads for downloading. This limit should be equal to or less than the number of CPU cores on your machine.
3. Next, we add code to login into azure and call the above function. Update the DownloadBlobs.ps1 file with below code.
### *** This script is to download blobs from an Azure storage account. *** ###
$subscriptionId = "cqewdjbewhdvqdhqdqdh"
$storageAccountName = "devsa"
$storageAccountKey = "q5u823hbdhdbeddvhqwdh"
$containerName = "orders"
$downloadLocation = "/users/shared/"
# Login
Connect-AzAccount
# Set Subscription
Set-AzContext -Subscription $subscriptionId
# Create storage context
$storageContext = New-AzStorageContext -StorageAccountName $storageAccountName -StorageAccountKey $storageAccountKey
# Download the blobs
DownloadBlobs -storageContext $storageContext -containerName $containerName -downloadLocation $downloadLocation -lastModifedInDays -2
# Function to download the blobs
function DownloadBlobs() {
param(
[Parameter(Mandatory=$true)][Object]$storageContext,
[Parameter(Mandatory=$true)][string]$containerName,
[Parameter(Mandatory=$true)][string]$downloadLocation,
[Parameter(Mandatory=$true)][Int32][ValidateScript({$_ -lt 0})] [int]$lastModifedInDays
)
$throttleLimit = 4
# Get the list of blobs
$blobList = Get-AzStorageBlob -Container $containerName -Context $storageContext
Write-Output "Total blobs in the container: " $blobList.Count
# Select blobs that has been modifed in last $lastModifedInDays days
$filteredBlobs = $blobList | Where-Object { $_.LastModified -gt ((Get-Date).Date).AddDays($days) }
Write-Output "Total blobs matching criteria: " $filteredBlobs.Count
Write-Output "Downloading...."
$filteredBlobs | ForEach-Object -Parallel {
Get-AzStorageBlobContent -Container $using:containerName -Blob $_.Name -Context $using:storageContext -Destination $using:downloadLocation
} -ThrottleLimit $throttleLimit
Write-Output "Download Complete"
}