-
Notifications
You must be signed in to change notification settings - Fork 0
Hosting
The Hosting feature of LittleBlocks enables the developers to configure the API on an IIS/Kestrel or a Windows Service host. Here are some samples of the configuration for different scenarios:
IIS/Kestrel Hosting
The HostAsWeb enables users to Run the host with predefined configuration as sync (Run Method) and async (Run Async Method) for specific Startup class. It also gives the flexibility to configure the logging
- For different environment
- To Define the rolling log file location
- Use log provider(s) (Such as Loggly, Seq, Azure Log & Analytics, etc)
public class Program
{
public static void Main(string[] args)
{
HostAsWeb.Run<Startup>(s =>
{
if (s.Environment.IsDevelopment() || s.Environment.IsEnvironment("Integration"))
return s.ConfigureLogger<Startup>(c => c.UseSeq(s.Configuration.GetSection("Logging:Seq")));
return s.ConfigureLogger<Startup>(c
=> c.SaveLogsTo("c:\\logs"), c =>
c.UseLoggly(s.Configuration.GetSection("Logging:Loggly"))
.UseSeq(s.Configuration.GetSection("Logging:Seq")));
}, args);
}
}
NOTE: The LittleBlocks.Sample.WebAPI project contains a sample of the configuration.
Windows Service Hosting
The LittleBlocks.Sample.WindowsService project contains a sample on who to host the API as windows service however the final product needs to be registered as a service and the PowerShell (install.ps1) that shows how we can install it.
class Program
{
static void Main(string[] args)
{
var logger = ConfigureLogger();
logger.Information("Start the service");
try
{
HostAsWindowsService.Run<Startup>(c =>
c.ConfigureLogger<Startup>(s => s.SaveLogsTo(BaseLogLocation)), args);
}
catch (Exception e)
{
logger.Error("{@e}", e);
throw;
}
}
private static ILogger ConfigureLogger()
{
var location = $@"{BaseLogLocation}\service-startup-{{Date}}.log";
var logger = new LoggerConfiguration()
.Enrich.WithThreadId()
.Enrich.WithMachineName()
.Enrich.WithEnvironmentUserName()
.Enrich.WithProcessId()
.Enrich.WithProcessName()
.WriteTo.RollingFile(location)
.CreateLogger();
Log.Logger = logger;
return logger;
}
private static string BaseLogLocation => $@"{AppContext.BaseDirectory}\logs";
}