コード例 #1
0
        public static string BuildLog4Net(Log4NetConfiguration configuration = null)
        {
            log4net.Layout.SerializedLayout layout = new log4net.Layout.SerializedLayout();
            layout.AddDecorator(new log4net.Layout.Decorators.StandardTypesDecorator());
            layout.AddDefault("");
            layout.AddRemove("message");
            layout.AddMember("messageobject");
            layout.ActivateOptions();

            log4net.Filter.LevelMatchFilter filter = new log4net.Filter.LevelMatchFilter
            {
                LevelToMatch = log4net.Core.Level.All
            };
            filter.ActivateOptions();

            (string InstanceName, bool Buffered, bool KeepFileOpen, bool Shared) = configuration ?? new Log4NetConfiguration();

            log4net.Appender.FileAppender.LockingModelBase LockingModel = new log4net.Appender.FileAppender.MinimalLock();
            if (KeepFileOpen)
            {
                if (Shared)
                {
                    LockingModel = new log4net.Appender.FileAppender.InterProcessLock();
                }
                else
                {
                    LockingModel = new log4net.Appender.FileAppender.ExclusiveLock();
                }
            }

            bool ImmediateFlush = !Buffered;

            log4net.Appender.RollingFileAppender rollingFileAppender = new log4net.Appender.RollingFileAppender
            {
                File = $@"log4net\{InstanceName.ToLower()}-{DateTime.Now.ToString("yyyyMMddHHmm")}-latest.log",

                ImmediateFlush = ImmediateFlush,
                LockingModel   = LockingModel,

                AppendToFile = true,
                RollingStyle = log4net.Appender.RollingFileAppender.RollingMode.Composite,
                DatePattern  = "yyyyMMddhhmm",
                MaxFileSize  = 100_000_000,
                Name         = $"{InstanceName}Appender",
                Layout       = layout
            };
            rollingFileAppender.AddFilter(filter);
            rollingFileAppender.ActivateOptions();

            string Repository = $"{InstanceName}Repository";

            log4net.Repository.ILoggerRepository repository = log4net.Core.LoggerManager.CreateRepository(Repository);

            log4net.Config.BasicConfigurator.Configure(repository, rollingFileAppender);

            return(Repository);
        }
    }