Sure thing (and thanks for the help).
Attached are 2 sample projects (basically the web template + 2 different trials with Serilog). I also added a screenshot of the mixed output.
First project with Serilog without the LogManager (what you saw in the previous post with the using included):
Project file
<Project Sdk="Microsoft.NET.Sdk.Web">
<PropertyGroup>
<TargetFramework>net5.0</TargetFramework>
</PropertyGroup>
<ItemGroup>
<Folder Include="wwwroot\" />
</ItemGroup>
<ItemGroup>
<PackageReference Include="Microsoft.Extensions.Configuration.Json" Version="5.0.0" />
<PackageReference Include="Rollbar" Version="4.0.3" />
<PackageReference Include="Rollbar.PlugIns.Serilog" Version="4.0.3" />
<PackageReference Include="Serilog.AspNetCore" Version="4.1.0" />
<PackageReference Include="Serilog.Settings.Configuration" Version="3.1.0" />
<PackageReference Include="Serilog.Sinks.Console" Version="4.0.0" />
<PackageReference Include="Serilog.Sinks.File" Version="5.0.0" />
<PackageReference Include="ServiceStack" Version="5.*" />
<PackageReference Include="ServiceStack.Logging.Serilog" Version="5.11.1" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\AgApi.ServiceInterface\AgApi.ServiceInterface.csproj" />
<ProjectReference Include="..\AgApi.ServiceModel\AgApi.ServiceModel.csproj" />
</ItemGroup>
</Project>
Program.cs
using System;
using Microsoft.AspNetCore;
using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Logging;
using Serilog;
using ServiceStack;
using ServiceStack.Logging;
using ServiceStack.Logging.Serilog;
namespace AgApi
{
public class Program
{
public static void Main(string[] args)
{
Log.Logger = new LoggerConfiguration()
.Enrich.FromLogContext()
.WriteTo.Console()
.CreateLogger();
try
{
Log.Information("Starting up");
CreateHostBuilder(args).Build().Run();
}
catch (Exception ex)
{
Log.Fatal(ex, "Application start-up failed");
}
finally
{
Log.CloseAndFlush();
}
}
public static IHostBuilder CreateHostBuilder(string[] args) =>
Host.CreateDefaultBuilder(args)
.UseSerilog()
.ConfigureWebHostDefaults(
builder => { builder.UseModularStartup<Startup>(); });
}
}
Startup and AppHost
using System;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.DependencyInjection;
using Funq;
using ServiceStack;
using ServiceStack.Logging;
using ServiceStack.Logging.Serilog;
using Serilog;
using AgApi.ServiceInterface;
using Rollbar;
using Rollbar.DTOs;
using Rollbar.PlugIns.Serilog;
using RollbarBase;
using Serilog.Events;
namespace AgApi
{
public class Startup : ModularStartup
{
private static ILog Log = LogManager.GetLogger(typeof(Startup));
// This method gets called by the runtime. Use this method to add services to the container.
// For more information on how to configure your application, visit https://go.microsoft.com/fwlink/?LinkID=398940
public new void ConfigureServices(IServiceCollection services)
{
}
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
app.UseServiceStack(new AppHost
{
AppSettings = new NetCoreAppSettings(Configuration)
});
app.UseSerilogRequestLogging();
Log.Info("Warning in Startup.Configure");
}
}
public class AppHost : AppHostBase
{
private static ILog Log = LogManager.GetLogger(typeof(AppHost));
public AppHost() : base("AgApi", typeof(MyServices).Assembly) { }
// Configure your AppHost with the necessary configuration and dependencies your App needs
public override void Configure(Container container)
{
SetConfig(new HostConfig
{
DebugMode = AppSettings.Get(nameof(HostConfig.DebugMode), false),
});
Log.Info("Warning in AppHost.");
}
}
}
Settings
{
"DebugMode": false,
"Logging": {
"IncludeScopes": false,
"Debug": {
"LogLevel": {
"Default": "Information",
"System": "Information",
"Microsoft": "Information"
}
},
"Console": {
"LogLevel": {
"Default": "Debug",
"System": "Information",
"Microsoft": "Information"
}
}
}
}
Second project giving the mixed output (and a screenshot showing the mixed ouput):
Project file
<Project Sdk="Microsoft.NET.Sdk.Web">
<PropertyGroup>
<TargetFramework>net5.0</TargetFramework>
</PropertyGroup>
<ItemGroup>
<Folder Include="wwwroot\" />
</ItemGroup>
<ItemGroup>
<PackageReference Include="Microsoft.Extensions.Configuration.Json" Version="5.0.0" />
<PackageReference Include="Serilog.Settings.Configuration" Version="3.1.0" />
<PackageReference Include="Serilog.Sinks.Console" Version="4.0.0" />
<PackageReference Include="Serilog.Sinks.File" Version="5.0.0" />
<PackageReference Include="ServiceStack" Version="5.*" />
<PackageReference Include="ServiceStack.Logging.Serilog" Version="5.11.1" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\..\..\ceesharp-library\ServiceStackBase\ServiceStackBase\ServiceStackBase.csproj" />
<ProjectReference Include="..\AgApi.ServiceInterface\AgApi.ServiceInterface.csproj" />
<ProjectReference Include="..\AgApi.ServiceModel\AgApi.ServiceModel.csproj" />
</ItemGroup>
</Project>
Program.cs
using System;
using Microsoft.AspNetCore;
using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Logging;
using ServiceStack;
namespace AgApi
{
public class Program
{
public static void Main(string[] args)
{
CreateHostBuilder(args).Build().Run();
}
public static IHostBuilder CreateHostBuilder(string[] args) =>
Host.CreateDefaultBuilder(args)
.ConfigureWebHostDefaults(
builder => { builder.UseModularStartup<Startup>(); });
}
}
Startup and AppHost
using System;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.DependencyInjection;
using Funq;
using ServiceStack;
using ServiceStack.Logging;
using ServiceStack.Logging.Serilog;
using Serilog;
using AgApi.ServiceInterface;
namespace AgApi
{
public class Startup : ModularStartup
{
private static ILog Log = LogManager.GetLogger(typeof(Startup));
// This method gets called by the runtime. Use this method to add services to the container.
// For more information on how to configure your application, visit https://go.microsoft.com/fwlink/?LinkID=398940
public new void ConfigureServices(IServiceCollection services)
{
}
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}
app.UseServiceStack(new AppHost
{
AppSettings = new NetCoreAppSettings(Configuration)
});
Log.Info("In Startup.Configure");
}
}
public class AppHost : AppHostBase
{
private static ILog Log = LogManager.GetLogger(typeof(AppHost));
public AppHost() : base("AgApi", typeof(MyServices).Assembly) { }
// Configure your AppHost with the necessary configuration and dependencies your App needs
public override void Configure(Container container)
{
SetConfig(new HostConfig
{
DebugMode = AppSettings.Get(nameof(HostConfig.DebugMode), false)
});
var logger = new LoggerConfiguration()
.WriteTo.Console()
.WriteTo.File("log-.txt", rollingInterval: RollingInterval.Day)
.CreateLogger();
LogManager.LogFactory = new SerilogFactory(logger);
Log.Info("In AppHost.Configure");
}
}
}
Settings
{
"DebugMode": false,
"Logging": {
"IncludeScopes": false,
"Debug": {
"LogLevel": {
"Default": "Information",
"System": "Information",
"Microsoft": "Information"
}
},
"Console": {
"LogLevel": {
"Default": "Debug",
"System": "Information",
"Microsoft": "Information"
}
}
}
}
Output:
Is there a way to use x to mix in the right log provider when a new project is created?
Thanks!