コード例 #1
0
        public static void ConfigureLogging(bool enableDebugLogging,
                                            bool logToFile,
                                            IHsController?hsController = null)
        {
            var config = new LoggerConfiguration().WriteTo.Console();

            config = enableDebugLogging ? config.MinimumLevel.Debug() : config.MinimumLevel.Information();

            if (hsController != null)
            {
                var hsTarget = new HomeSeerTarget(hsController);
                config = config.WriteTo.Sink(hsTarget);
            }

            if (logToFile)
            {
                string codeBase = new Uri(typeof(Logger).Assembly.CodeBase).LocalPath;
                string hsDir    = Path.GetDirectoryName(codeBase);
                string logFile  = Path.Combine(hsDir, "Logs", PlugInData.PlugInId, "file.log");

                config = config.WriteTo.File(logFile, fileSizeLimitBytes: 10 * 1024 * 1024);
            }
            config = config.Destructure.UsingAttributes();

            Log.Logger = config.CreateLogger();
        }
コード例 #2
0
        public static void ConfigureLogging(bool enableDebugLogging,
                                            bool logToFile,
                                            IHsController?hsController = null)
        {
            var config = new NLog.Config.LoggingConfiguration
            {
                DefaultCultureInfo = CultureInfo.InvariantCulture
            };
            var logconsole = new ConsoleTarget("logconsole");

            LogLevel minLevel = enableDebugLogging ? LogLevel.Debug : LogLevel.Info;

            config.AddRule(enableDebugLogging ? LogLevel.Debug : LogLevel.Info, LogLevel.Fatal, logconsole);

            if (hsController != null)
            {
                var hsTarget = new HomeSeerTarget(hsController);
                config.AddRule(minLevel, LogLevel.Fatal, hsTarget);
            }

            if (logToFile)
            {
                string codeBase = new Uri(typeof(Logger).Assembly.CodeBase).LocalPath;
                string hsDir    = Path.GetDirectoryName(codeBase);
                string logFile  = Path.Combine(hsDir, "logs", PlugInData.PlugInId, "file.log");

                var fileTarget = new FileTarget()
                {
                    FileNameKind     = FilePathKind.Absolute,
                    CreateDirs       = true,
                    MaxArchiveDays   = 7,
                    MaxArchiveFiles  = 7,
                    ArchiveEvery     = FileArchivePeriod.Day,
                    ArchiveAboveSize = 10 * 1024 * 1024,
                    ConcurrentWrites = false,
                    ArchiveNumbering = ArchiveNumberingMode.Rolling,
                    FileName         = logFile,
                    KeepFileOpen     = true,
                };

                config.AddRule(minLevel, LogLevel.Fatal, fileTarget);
            }
            NLog.LogManager.Configuration = config;
        }