예제 #1
0
 public NLogTests(NLogConfiguration configuration = null)
 {
     factory = LoggerBuilders.BuildNLogFactory(configuration ?? new NLogConfiguration {
         KeepFileOpen = true
     });
     logger = factory.GetLogger(nameof(IAuditLogger));
 }
예제 #2
0
        public static NLog.LogFactory BuildNLogFactory(NLogConfiguration configuration = null)
        {
            (bool Buffered, bool KeepFileOpen, bool Shared) = configuration ?? new NLogConfiguration();

            bool AutoFlush = !Buffered;

            NLog.Targets.FileTarget fileTarget = new NLog.Targets.FileTarget("audit-log")
            {
                FileName        = $"./nlog/audit-{DateTime.Now.ToString("yyyyMMddHHmm")}-latest.log",
                ArchiveFileName = $"./nlog/audit-{DateTime.Now.ToString("yyyyMMddHHmm")}-{{###}}.log",

                AutoFlush        = AutoFlush,
                KeepFileOpen     = KeepFileOpen,
                ConcurrentWrites = Shared,

                ArchiveEvery     = NLog.Targets.FileArchivePeriod.Day,
                ArchiveNumbering = NLog.Targets.ArchiveNumberingMode.Rolling,
                ArchiveAboveSize = 100_000_000,
                Layout           = new NLog.Layouts.JsonLayout
                {
                    IncludeAllProperties = true,
                    Attributes           =
                    {
                        new NLog.Layouts.JsonAttribute("@t",  "${longdate}")
                        {
                            Encode = false
                        },
                        new NLog.Layouts.JsonAttribute("@l",  "${level:upperCase=true}")
                        {
                            Encode = false
                        },
                        new NLog.Layouts.JsonAttribute("@mt", "${message:raw=true}"),
                    }
                }
            };

            NLog.Config.LoggingConfiguration loggingConfiguration = new NLog.Config.LoggingConfiguration();

            loggingConfiguration.AddTarget(fileTarget);

            loggingConfiguration.AddRuleForOneLevel(NLog.LogLevel.Info, fileTarget);

            return(new NLog.LogFactory(loggingConfiguration)
            {
                ThrowExceptions = true,
                ThrowConfigExceptions = true,
            });
        }