Aufgrund des Ziels sollte es ein Microservice sein, ich verwende das CQRS-Muster.
Hier ist meine Startup.cs
Code: Select all
private readonly ISession session;
private readonly ILogger logger;
public Startup(IConfiguration configuration)
{
Configuration = configuration;
var baseDirectory = Directory.GetDirectoryRoot(AppDomain.CurrentDomain.BaseDirectory);
Log.Logger = new LoggerConfiguration()
.WriteTo
.Logger(lc => lc.Filter
.ByIncludingOnly(e => e.Level == LogEventLevel.Information || e.Level == LogEventLevel.Debug)
.WriteTo.RollingFile(Path.Combine(baseDirectory, "/Logs/info.log")))
.WriteTo
.Logger(lc => lc.Filter
.ByIncludingOnly(e => e.Level == LogEventLevel.Error)
.WriteTo.RollingFile(Path.Combine(baseDirectory, "/Logs/error.log")))
}
public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory)
{
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}
loggerFactory.AddSerilog();
...//To shorten this example in Stackoverflow, I've excluded the standard code. But in reality they are of course there
}
Code: Select all
public MaterialCommandHandler(ISession session, ILogger logger)
{
this.session = session;
this.logger = logger;
}
Code: Select all
public async Task Handle(CreateMaterialCommand command)
{
var material = new Material(command.Id, command.TechDesc, command.Description, command.MaterialClass);
logger.LogDebug($"{DateTimeOffset.UtcNow} - {nameof(CreateMaterialCommand)} - received for {material} with {nameof(command.TechDesc)} : {command.TechDesc}.");
await session.Add(material);
await session.Commit();
logger.LogDebug($"{DateTimeOffset.UtcNow} - {nameof(CreateMaterialCommand)} - received for {material} with {nameof(command.TechDesc)} : {command.TechDesc}.");
}
Mobile version