This is a project template for .Net Core 3.1 Console app, pre-configured Autofac, Serilog, AutoMapper, Newtonsoft.Json using
dotnet new -i Superwalnut.NetCoreConsoleTemplate
to install project as a template, And usingdotnet new core-console-autofac
to create a project with the template.
Pre-request
- .Net Core 3.1
- Autofac
- Serilog
- AutoMapper
This is a development project template, it is NOT production ready. It only kickstarting your project so that you don't need to build from scratch.
- pre-Configured Autofac 5.2.0 registrations setup.
- pre-Configured Serilog 2.9.0 console sinks.
- pre-Configured AutoMapper 10.0.0.
- pre-Installed Newtonsoft.Json 12.0.3
- Using
dotnet new -i <package>
to install the project template from nuget Superwalnut.NetCoreConsoleTemplate
$ dotnet new -i Superwalnut.NetCoreConsoleTemplate
You should see '.Net Core Console Autofac+Serilog' in your template list by dotnet new -l
- Using
dotnet new core-console-autofac -n <your-project-name>
to create a project with your own project name using this template
$ dotnet new core-console-autofac -n NetCoreDemo -o NetCoreDemo
This creates a project in folder NetCoreDemo
ContainerBuilder() that you can populate IServiceCollection and register your autofac modules in Startup.cs. Taking ConsoleModule from the template as an example, you can also create modules for your Service Layer, Repository Layer, etc.
public IServiceProvider ConfigureServices(IServiceCollection services)
{
...
builder.RegisterModule<ConsoleModule>();
var container = builder.Build();
return container.Resolve<IServiceProvider>();
}
Serilog & Automapper are registered in the ConsoleModule,
public class ConsoleModule : Module
{
protected override void Load(ContainerBuilder builder)
{
builder.AddAutoMapper(x=>x.AddProfile<MyAutoMapperProfile>());
}
}
With console sinks in the appsettings.json
"Serilog": {
"Using": [ "Serilog.Sinks.Console" ],
"MinimumLevel": "Information",
"WriteTo": [
{
"Name": "Console"
}
]
}
And Serilog configuration in the startup.cs
public static void CreateLogger(IConfigurationRoot configuration)
{
Log.Logger = new LoggerConfiguration()
.MinimumLevel.Debug()
.MinimumLevel.Override("Microsoft", LogEventLevel.Information)
.Enrich.FromLogContext()
.ReadFrom.Configuration(configuration)
.CreateLogger();
}
With created example profile,
public class MyAutoMapperProfile : Profile
{
public MyAutoMapperProfile()
{
CreateMap<Foo, FooDto>();
...
}
}
And register the profile in ConsoleModule for AutoMapper,
public class ConsoleModule : Module
{
protected override void Load(ContainerBuilder builder)
{
...
builder.AddAutoMapper(x=>x.AddProfile<MyAutoMapperProfile>());
}
}
Reach out to me at one of the following places!