/// <summary> /// Configures Sejil logging. /// </summary> /// <param name="builder">The host builder to configure.</param> /// <param name="url">The URL at which Sejil should be available. Defaults to '/sejil'.</param> /// <param name="minLogLevel">The minimum log level.</param> /// <param name="writeToProviders"> /// By default, Serilog does not write events to Microsoft.Extensions.Logging.ILoggerProviders /// registered through the Microsoft.Extensions.Logging API. Specify true to write events to /// all providers. /// </param> /// <param name="sinks"> /// Configures additional sinks that log events will be emitted to. Use this to enable other serilog providers: /// <code> /// UseSejil(sinks: sinks => /// { /// sinks.Console(); /// sinks.ElmahIo(new ElmahIoSinkOptions("API_KEY", new Guid("LOG_ID"))); /// // ... /// }); /// </code> /// </param> /// <returns>The host builder.</returns> public static IHostBuilder UseSejil( this IHostBuilder builder, string url = "/sejil", LogLevel minLogLevel = LogLevel.Information, bool writeToProviders = false, Action <LoggerSinkConfiguration> sinks = null) { var settings = new SejilSettings(url, IWebHostBuilderExtensions.MapSerilogLogLevel(minLogLevel)); return(builder .UseSerilog((context, cfg) => { cfg .Enrich.FromLogContext() .ReadFrom.Configuration(context.Configuration) .MinimumLevel.ControlledBy(settings.LoggingLevelSwitch) .WriteTo.Sejil(settings); sinks?.Invoke(cfg.WriteTo); }, writeToProviders: writeToProviders) .ConfigureServices((_, services) => { services.TryAddSingleton <IHttpContextAccessor, HttpContextAccessor>(); services.AddSingleton <ISejilSettings>(settings); services.AddScoped <ISejilRepository, SejilRepository>(); services.AddScoped <ISejilSqlProvider, SejilSqlProvider>(); services.AddScoped <ISejilController, SejilController>(); })); }
public void Ctor_saves_specified_url() { // Arrange & act var settings = new SejilSettings("/url", LogEventLevel.Debug); // Assert Assert.Equal("/url", settings.Url); }
public void Ctor_save_and_adds_a_leading_slash_to_specified_url_when_missing() { // Arrange & act var settings = new SejilSettings("url", LogEventLevel.Debug); // Assert Assert.Equal("/url", settings.Url); }
public void Ctor_loads_app_html() { // Arrange & act var settings = new SejilSettings("url", LogEventLevel.Debug); // Assert Assert.Equal(ResourceHelper.GetEmbeddedResource("SejilSQL.index.html"), settings.SejilAppHtml); }
public void Ctor_sets_default_db_name() { // Arrange & act var settings = new SejilSettings("", LogEventLevel.Debug); // Assert Assert.Matches(@"^Sejil-[0-9A-F]{8}-([0-9A-F]{4}-){3}[0-9A-F]{12}\.sqlite$", Path.GetFileName(settings.SqliteDbPath)); }
public void Ctor_sets_inital_min_log_level() { // Arrange & act var initalLogLevel = LogEventLevel.Debug; var settings = new SejilSettings("/url", initalLogLevel); // Assert Assert.Equal(initalLogLevel, settings.LoggingLevelSwitch.MinimumLevel); }
public void Ctor_sets_default_settings() { // Arrange & act var settings = new SejilSettings("", LogEventLevel.Debug); // Assert Assert.Equal(new[] { "message", "level", "timestamp", "exception", "sourceapp" }, settings.NonPropertyColumns); Assert.Equal(100, settings.PageSize); }
public void Ctor_sets_db_path_to_localAppData_when_running_outside_Azure() { // Arrange & act var basePath = Path.Combine( Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData), "testhost"); var settings = new SejilSettings("", LogEventLevel.Debug); // Assert Assert.Equal(basePath, Path.GetDirectoryName(settings.SqliteDbPath)); }
public void Ctor_sets_db_path_to_home_folder_when_running_inside_Azure() { // Arrange & act // Azure websites will have this env. variable var basePath = Path.Combine(Path.GetPathRoot(Environment.CurrentDirectory), "home"); Environment.SetEnvironmentVariable("WEBSITE_SITE_NAME", "value", EnvironmentVariableTarget.Process); var settings = new SejilSettings("", LogEventLevel.Debug); // Assert Assert.Equal(basePath, Path.GetDirectoryName(settings.SqliteDbPath)); Environment.SetEnvironmentVariable("WEBSITE_SITE_NAME", string.Empty, EnvironmentVariableTarget.Process); }
/// <summary> /// Adds Sejil services. /// </summary> /// <param name="builder">The web host builder.</param> /// <param name="url">The URL at which Sejil should be available.</param> /// <param name="minLogLevel">The minimum log level.</param> /// <returns></returns> public static IWebHostBuilder AddSejil(this IWebHostBuilder builder, string url, LogLevel minLogLevel) { var settings = new SejilSettings(url, MapSerilogLogLevel(minLogLevel)); return(builder .ConfigureLogging((logging) => logging.AddSerilog(CreateLogger(settings))) .ConfigureServices(services => { services.TryAddSingleton <IHttpContextAccessor, HttpContextAccessor>(); services.AddSingleton <ISejilSettings>(settings); services.AddScoped <ISejilRepository, SejilRepository>(); services.AddScoped <ISejilSqlProvider, SejilSqlProvider>(); services.AddScoped <ISejilController, SejilController>(); })); }
public void ConfigureSejil_executes_setupAction() { // Arrange var authScheme = "authScheme"; var title = "title"; var settings = new SejilSettings("/sejil", LogEventLevel.Debug); var services = new ServiceCollection().AddSingleton <ISejilSettings>(settings); // Act services.ConfigureSejil(options => { options.AuthenticationScheme = authScheme; options.Title = title; }); // Assert Assert.Equal(authScheme, settings.AuthenticationScheme); Assert.Equal(title, settings.Title); }
public void TrySetMinimumLogLevel_attempts_to_sets_specified_min_log_level(string logLevel, LogEventLevel expected, bool expectedResult) { // Arrange var initialLogLevel = LogEventLevel.Debug; var settings = new SejilSettings("", initialLogLevel); // Act var result = settings.TrySetMinimumLogLevel(logLevel); // Assert Assert.Equal(expectedResult, result); if (result) { Assert.Equal(expected, settings.LoggingLevelSwitch.MinimumLevel); } else { Assert.Equal(initialLogLevel, settings.LoggingLevelSwitch.MinimumLevel); } }