public static Microsoft.Extensions.Logging.ILoggerFactory GetLoggerFactory()
        {
            var fac = new Microsoft.Extensions.Logging.LoggerFactory();

            fac.AddSerilog();
            return(fac);
        }
Exemple #2
0
        /// <summary>
        ///  Adds a console logger. Can be omitted if internal SIPSorcery debug and warning messages are not required.
        /// </summary>
        private static void AddConsoleLogger()
        {
            var loggerFactory = new Microsoft.Extensions.Logging.LoggerFactory();
            var loggerConfig  = new LoggerConfiguration()
                                .Enrich.FromLogContext()
                                .MinimumLevel.Is(Serilog.Events.LogEventLevel.Information)
                                .WriteTo.Console()
                                .CreateLogger();

            loggerFactory.AddSerilog(loggerConfig);
            SIPSorcery.Sys.Log.LoggerFactory = loggerFactory;
        }
        /// <summary>
        /// Setup and configuration of the <see cref="LogManager"/> with a custom serilog built configuration.
        /// </summary>
        /// <param name="configuration">The Serilog configure to be loaded.</param>
        /// <exception cref="ArgumentNullException">Thrown when a null configuration is provided.</exception>
        public static void LoadLogManager(LoggerConfiguration configuration)
        {
            if (configuration == null)
            {
                throw new ArgumentNullException(nameof(configuration));
            }
            var logger        = configuration.CreateLogger();
            var loggerFactory = new Microsoft.Extensions.Logging.LoggerFactory();
            var loadedFactory = loggerFactory.AddSerilog(logger);

            LogManager.InitalizeManager(loadedFactory);
        }
Exemple #4
0
        public static Microsoft.Extensions.Logging.ILogger InitTestLogger(Xunit.Abstractions.ITestOutputHelper output)
        {
#if DEBUG
            string template = "{Timestamp:yyyy-MM-dd HH:mm:ss.ffff} [{Level}] {Scope} {Message}{NewLine}{Exception}";
            //string template = "{Timestamp:yyyy-MM-dd HH:mm:ss.ffff} [{Level}] ({ThreadId:000}){Scope} {Message}{NewLine}{Exception}";
            var loggerFactory = new Microsoft.Extensions.Logging.LoggerFactory();
            var loggerConfig  = new LoggerConfiguration()
                                .MinimumLevel.Verbose()
                                .Enrich.WithProperty("ThreadId", System.Threading.Thread.CurrentThread.ManagedThreadId)
                                .WriteTo.TestOutput(output, outputTemplate: template)
                                .WriteTo.Console(outputTemplate: template)
                                .CreateLogger();
            loggerFactory.AddSerilog(loggerConfig);

            SIPSorcery.Sys.Log.LoggerFactory = loggerFactory;
#endif
            return(SIPSorcery.Sys.Log.Logger);
        }
        /// <summary>
        /// Setup and configuration of the <see cref="LogManager"/> with the entire configuration being loaded from the config file.
        /// </summary>
        /// <param name="configuration">The loaded application settings from the hosting app.settings file.</param>
        /// <param name="includeMachineName">Optional parameter that determines if the machine name will be included in the logging, this is set to a default value of true.</param>
        /// <param name="includeThreadId">Optional parameter that determines if the executing thread id will be included in the logging, this is set to a default value of false.</param>
        /// <param name="includeThreadName">Optional parameter that determines if the executing thread name will be included in the logging, this is set to a default value of false. </param>
        ///
        public static void LoadLogManager(IConfiguration configuration, bool includeMachineName = true, bool includeThreadId = true, bool includeThreadName = false)
        {
            // instantiate and configure logging. Using serilog here, to log to console and a text-file.
            var loggerFactory = new Microsoft.Extensions.Logging.LoggerFactory();
            var loggerConfig  = LoadConfiguration(configuration);

            if (includeMachineName)
            {
                loggerConfig.Enrich.WithMachineName();
            }
            if (includeThreadId)
            {
                loggerConfig.Enrich.WithThreadId();
            }
            if (includeThreadName)
            {
                loggerConfig.Enrich.WithThreadName();
            }
            var logger        = loggerConfig.CreateLogger();
            var loadedFactory = loggerFactory.AddSerilog(logger);

            LogManager.InitalizeManager(loadedFactory);
        }