protected void Application_Start(object sender, EventArgs e)
{
Environment.SetEnvironmentVariable("BASEDIR", AppDomain.CurrentDomain.BaseDirectory);
var log = new LoggerConfiguration()
//.WriteTo.File()
.ReadFrom.AppSettings()
// .Enrich.With(new UsernameEnricher())
.MinimumLevel.Debug()
.Filter.With(new KeepAliveFilter())
.CreateLogger();
Log.Logger = log;
LogManager.LogFactory = new SerilogFactory(log);
Log.Information(string.Empty);
Log.Information("==== Application started ====");
AppHost.Start();
}
namespace ServiceStack.Logging.Serilog
{
/// <summary>
/// ILogFactory that creates an Serilog ILog logger
/// </summary>
public class SerilogFactory : ILogFactory
{
private readonly ILogger _log;
/// <summary>
/// Initializes a new instance of the <see cref="SerilogFactory"/> class.
/// </summary>
public SerilogFactory(ILogger log)
{
_log = log;
}
/// <summary>
/// Gets the logger.
/// </summary>
/// <param name="type">The type.</param>
/// <returns></returns>
public ILog GetLogger(Type type)
{
return new SerilogLogger(_log.ForContext(type));
}
/// <summary>
/// Gets the logger.
/// </summary>
/// <param name="typeName">Name of the type.</param>
/// <returns></returns>
public ILog GetLogger(string typeName)
{
try
{
var type = Type.GetType(typeName);
return new SerilogLogger(_log.ForContext(type));
}
catch (Exception)
{
// if the type is not valid, just return a non-context logger
return new SerilogLogger(_log);
}
}
}