Пример #1
0
        private static LogLevel Convert(MsixHeroLogLevel level)
        {
            switch (level)
            {
            case MsixHeroLogLevel.Trace:
                return(LogLevel.Trace);

            case MsixHeroLogLevel.Debug:
                return(LogLevel.Debug);

            case MsixHeroLogLevel.Info:
                return(LogLevel.Info);

            case MsixHeroLogLevel.Warn:
                return(LogLevel.Warn);

            case MsixHeroLogLevel.Error:
                return(LogLevel.Error);

            case MsixHeroLogLevel.Fatal:
                return(LogLevel.Fatal);

            default:
                throw new ArgumentOutOfRangeException(nameof(level), level, null);
            }
        }
Пример #2
0
        public static void Initialize(MsixHeroLogLevel minLogLevel = MsixHeroLogLevel.Info, MsixHeroLogLevel maxLogLevel = MsixHeroLogLevel.Fatal)
        {
            var assembly           = Path.GetFileNameWithoutExtension((Assembly.GetEntryAssembly() ?? Assembly.GetExecutingAssembly()).Location);
            var fileName           = $"{DateTime.Now:yyyyMMdd-hhmmss}_{assembly}.log";
            var targetFileNameInfo = new FileInfo(Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData), "MSIX-Hero", "Logs", fileName));

            Initialize(targetFileNameInfo.FullName, minLogLevel, maxLogLevel);
        }
Пример #3
0
        public static void Initialize(string logFile, MsixHeroLogLevel minLogLevel = MsixHeroLogLevel.Debug, MsixHeroLogLevel maxLogLevel = MsixHeroLogLevel.Fatal)
        {
            if (string.IsNullOrEmpty(logFile))
            {
                throw new ArgumentNullException(nameof(logFile));
            }

            var config = new NLog.Config.LoggingConfiguration();

            var logfile = new NLog.Targets.FileTarget
            {
                FileName = logFile,
                Header   = GetHeader(),
                Layout   = "${longdate}\t${level:uppercase=true}\t${logger}\t${message}\t${exception:format=tostring}"
            };

            config.AddRule(Convert(minLogLevel), Convert(maxLogLevel), logfile);
            config.AddRule(Convert(minLogLevel), Convert(maxLogLevel), new ConsoleTarget());

            NLog.LogManager.Configuration = config;
        }
Пример #4
0
        public static void Initialize(string logFile, MsixHeroLogLevel minLogLevel = MsixHeroLogLevel.Debug, MsixHeroLogLevel maxLogLevel = MsixHeroLogLevel.Fatal)
        {
            if (string.IsNullOrEmpty(logFile))
            {
                throw new ArgumentNullException(nameof(logFile));
            }

            LogFile = logFile;

            var logFileInfo = new FileInfo(logFile);

            if (logFileInfo.Directory?.Exists != true)
            {
                logFileInfo.Directory?.Create();
            }
            else if (!logFileInfo.Exists)
            {
                File.WriteAllText(logFileInfo.FullName, string.Empty);
            }

            var config = new NLog.Config.LoggingConfiguration();

            var fileTarget = new FileTarget
            {
                FileName = logFile,
                Header   = GetHeader(),
                Layout   = "${longdate}\t${level:uppercase=true}\t${logger}\t${message}\t${exception:format=tostring}"
            };

            config.AddRule(Convert(minLogLevel), Convert(maxLogLevel), fileTarget);

            config.AddRule(LogLevel.Warn, LogLevel.Fatal, new ColoredConsoleTarget
            {
                Layout = "${message}",
                DetectConsoleAvailable = true
            });

            NLog.LogManager.Configuration = config;
        }