Skip to content

Instantly share code, notes, and snippets.

View MahdiKarimipour's full-sized avatar
🎯
Focusing

Mahdi Karimipour MahdiKarimipour

🎯
Focusing
  • TechnologyLeads
  • Sydney, Australia
View GitHub Profile
@MahdiKarimipour
MahdiKarimipour / app-secrets-loading-at-startup-time.cs
Created June 25, 2021 01:09
App Secrets Load at Start Up Time
public static IHostBuilder CreateHostBuilder(string[] args) =>
Host.CreateDefaultBuilder(args)
.ConfigureWebHostDefaults(webBuilder =>
{
webBuilder.UseStartup<Startup>();
})
.ConfigureAppConfiguration((context, config) =>
{
if (context.HostingEnvironment.IsProduction())
{
public void Log<TState>(
Microsoft.Extensions.Logging.LogLevel logLevel,
EventId eventId,
TState state,
Exception exception,
Func<TState, Exception, string> formatter)
{
if (!IsEnabled(logLevel))
{
return;
@MahdiKarimipour
MahdiKarimipour / logging-nlog-config.xml
Last active August 4, 2021 04:02
NLog Configuration File
<?xml version="1.0" encoding="utf-8" ?>
<nlog xmlns="http://www.nlog-project.org/schemas/NLog.xsd"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
autoReload="true"
internalLogLevel="Info"
internalLogFile="${basedir}Logsinternal-nlog.txt">
<!-- enable asp.net core layout renderers -->
<extensions>
<add assembly="NLog.Web.AspNetCore" />
<add assembly="Microsoft.ApplicationInsights.NLogTarget" />
public class NLogger : Microsoft.Extensions.Logging.ILogger
{
private readonly Logger logger;
private readonly string name;
private readonly NloggerConfiguration nloggerConfiguration;
private readonly UserContext userContext;
private readonly RequestContext requestContext;
public NLogger(string name,
NloggerConfiguration nloggerConfiguration,
UserContext userContext,
@MahdiKarimipour
MahdiKarimipour / logging-nlogger-middleware-integration.cs
Created June 25, 2021 01:16
Integrating Nlogger to Asp.NET Middleware Pipleline
public void Configure(
IApplicationBuilder app,
IWebHostEnvironment env,
IApiVersionDescriptionProvider provider,
ILoggerFactory loggerFactory,
UserContext userContext,
RequestContext requestContext)
{
loggerFactory.AddProvider(
new LoggerProvider(new NloggerConfiguration(),
"Logging": {
"LogLevel": {
"Default": "Trace",
"Microsoft.AspNetCore.Hosting.Diagnostics": "Information",
"Microsoft.AspNetCore.Authentication": "Information",
"Microsoft.*": "Error",
"System.*": "Error"
}
}
@MahdiKarimipour
MahdiKarimipour / logging-sample-output.txt
Created June 25, 2021 01:18
ASP.NET Sample Log Output
2021-06-22 13:41:32.5948|INFO|Request finished HTTP/1.1 GET https://azure-api.azurewebsites.net/v1/healthcheck - - - 200 - - 0.6111ms |Microsoft.AspNetCore.Hosting.Diagnostics|UserId: |Channel: Unknown|Environment: Production|UserAgent: Azure Traffic Manager Endpoint Monitor|CorrelationId: CorrelationIdNotProvided|EventId: 2|url: |action:
public class CustomerService : ICustomerService
{
private readonly SampleApiDb database;
public CustomerService(SampleApiDb database)
{
this.database = database;
}
public async Task<string> CreateStripeCustomer(
CreateCustomerRequestViewModel request)
{
@MahdiKarimipour
MahdiKarimipour / unit-of-work-repository.cs
Created June 25, 2021 01:38
Unit of Work Repository
public class CustomerRepository : RepositoryBase<SampleDBContext>, IRespository<Guid, Customer>
{
public CustomerRepository(SubscriptionDBContext _context) : base(_context)
{
}
public void Create(Customer customer)
{
Context.Customers.Add(customer);
}
public void Delete(Guid id)
@MahdiKarimipour
MahdiKarimipour / unit-of-work-repository-base.cs
Created June 25, 2021 01:39
Unit of Work Repository Base
public abstract class RepositoryBase<TContext> where TContext : DbContext
{
public TContext Context { get; set; }
public RepositoryBase(TContext context)
{
Context = context;
}
public IQueryable<T2> Procedure<T2>(string name, params DbParameter[] parameters) where T2 : class
{
var sqlParameters = new List<SqlParameter>();