示例#1
0
        public static void ConfigureCustomLogging(HostBuilderContext hostingContext, ILoggingBuilder logging, IUserConfiguration userConfiguration)
        {
            logging.ClearProviders();

            if (userConfiguration.IsLoggingEnabled)
            {
                logging.AddConfiguration(hostingContext.Configuration.GetSection("Logging"));

                if (userConfiguration.IsLoggerEnabled(EnabledLoggersEnum.Debug))
                {
                    logging.AddDebug();
                }

                if (userConfiguration.IsLoggerEnabled(EnabledLoggersEnum.Console))
                {
                    logging.AddConsole();
                }

                if (userConfiguration.IsLoggerEnabled(EnabledLoggersEnum.File))
                {
                    // Must set the log name prior to adding Log4Net because it must know this value
                    // before it loads the config file. It does pattern matching and substitution on the filename.
                    string logName = $"{userConfiguration.LogName}.log";
                    if (!string.IsNullOrEmpty(userConfiguration.LogPath))
                    {
                        if (!Directory.Exists(userConfiguration.LogPath))
                        {
                            Directory.CreateDirectory(userConfiguration.LogPath);
                        }

                        logName = $"{userConfiguration.LogPath}\\{logName}";
                    }
                    log4net.GlobalContext.Properties["LogName"] = logName;
                    logging.AddLog4Net("log4net.config");
                }
            }
        }