Skip to content

Instantly share code, notes, and snippets.

Avatar

Jernej Kavka (JK) [SSW] jernejk

View GitHub Profile
View efcore-combine-logs-sql.cs
public async Task InsertTweetStoreProc(string username, string message, CancellationToken ct = default)
{
using (_logger.BeginScope(new Dictionary<string, object> { { "EFQueries", "GetTweetsLog" } }))
{
_ = await _context.Database
.ExecuteSqlRawAsync(
"InsertTweet @Username, @Message",
new SqlParameter("Username", username),
new SqlParameter("Message", message));
}
View execute-store-proc-efcore-5.cs
public async Task InsertTweetStoreProc(string username, string message, CancellationToken ct = default)
{
using (_logger.BeginScope(new Dictionary<string, object> { { "EFQueries", "InsertTweetStoreProc" } }))
{
_ = await _context.Database
.ExecuteSqlRawAsync(
"InsertTweet @Username, @Message",
new SqlParameter("Username", username),
new SqlParameter("Message", message));
}
@jernejk
jernejk / create-custom-form-recognizer.ps1
Last active July 19, 2021 08:16
Create Custom Form Recognizer script
View create-custom-form-recognizer.ps1
param(
[Parameter()]
[ValidateNotNullOrEmpty()]
[string] $ResourceGroup = $null,
[Parameter()]
[ValidateNotNullOrEmpty()]
[string] $ResourceLocation = 'EastUS',
# F0 is free and S0 is paid SKU
View efcore-combine-logs.cs
public async Task<IEnumerable<Tweet>> GetTweetsWithExtraLogs(CancellationToken ct = default)
{
using (_logger.BeginScope(new Dictionary<string, object> { { "EFQueries", "GetTweetsLog" } }))
{
return await _context.Tweets
.TagWith("GetTweets + LogContext")
.ToListAsync(ct)
.ConfigureAwait(false);
}
}
View efcore-log-scope-store-procs.cs
public Task InsertTweetStoreProc(string username, string message, CancellationToken ct = default)
{
using (_logger.BeginScope(new Dictionary<string, object> { { "EFQueries", "InsertTweetStoreProc" } }))
{
_ = _context.Tweets
.FromSqlRaw(
"InsertTweet @Username, @Message",
new SqlParameter("Username", username),
new SqlParameter("Message", message))
// A hack to make STORE PROC work when they don't return anything.
View efcore-log-scope-insert.cs
public async Task InsertTweet(string username, string message, CancellationToken ct = default)
{
using (_logger.BeginScope(new Dictionary<string, object> { { "EFQueries", "InsertTweet" } }))
{
_context.Tweets.Add(new Tweet
{
Username = username,
Message = message,
CreatedUtc = DateTime.UtcNow
});
View efcore-indirect-log-scope-query.cs
using (_logger.BeginScope(new Dictionary<string, object> { { "EFQueries", "GetTweets" } }))
{
var tweets = await _twitterService.GetTweets(ct);
}
// Same query as before.
public async Task<IEnumerable<Tweet>> GetTweets(CancellationToken ct = default)
{
return await _context.Tweets
.ToListAsync(ct)
View efcore-query-tag.cs
public async Task<IEnumerable<Tweet>> GetTweetsWithQueryTags(CancellationToken ct = default)
{
return await _context.Tweets
.TagWith("GetTweets")
.ToListAsync(ct)
.ConfigureAwait(false);
}
View insert-entity-no-log-scope.cs
public async Task InsertTweetWithoutLogScope(string username, string message, CancellationToken ct = default)
{
_context.Tweets.Add(new Tweet
{
Username = username,
Message = message
});
await _context.SaveChangesAsync(ct);
}