Exemplo n.º 1
0
        /// <summary>
        /// Gets the logger path for this file.
        /// </summary>
        /// <param name="name"></param>
        /// <param name="flags"></param>
        /// <returns></returns>
        private static string GetFileLoggerPath(string name, FileLoggerFlags flags)
        {
            if (name == null)
            {
                throw new ArgumentNullException("name");
            }
            if (name.Length == 0)
            {
                throw new ArgumentOutOfRangeException("'name' is zero-length.");
            }

            // get the path...
            string path = Runtime.Current.LogsFolderPath;

            // mbr - 2007-03-19 - support own folder?
            if ((int)(flags & FileLoggerFlags.OwnFolder) != 0)
            {
                path = Path.Combine(path, name);
            }

            // mbr - 11-10-2005 - add the date...
            if ((flags & FileLoggerFlags.AddDateToFileName) != 0)
            {
                name = name + " - " + DateTime.Now.ToString("yyyyMMdd");
            }

            // add...
            path = Path.Combine(path, name + ".log");

            // return...
            return(path);
        }
Exemplo n.º 2
0
 /// <summary>
 /// Creates a file logger with the given name.
 /// </summary>
 /// <param name="name"></param>
 /// <param name="formatter">Specify the formatter, or <c>null</c> to use the default formatter.</param>
 public static FileLog CreateFileLogger(string name, FileLoggerFlags flags, ILogFormatter formatter)
 {
     return(CreateFileLogger(name, flags, formatter, LogLevel.Debug, LogLevel.Fatal));
 }
Exemplo n.º 3
0
        /// <summary>
        /// Creates a file logger with the given name.
        /// </summary>
        /// <param name="name"></param>
        /// <param name="formatter">Specify the formatter, or <c>null</c> to use the default formatter.</param>
        public static FileLog CreateFileLogger(string name, FileLoggerFlags flags, ILogFormatter formatter,
                                               LogLevel minLevel, LogLevel maxLevel)
        {
            if (name == null || name.Length == 0)
            {
                name = "Log";
            }

            // mbr - 27-06-2007 - missed the lock...
            lock (_fileLogLock)
            {
                // get the file path...
                string path = GetFileLoggerPath(name, flags);
                if (path == null)
                {
                    throw new InvalidOperationException("'path' is null.");
                }
                if (path.Length == 0)
                {
                    throw new InvalidOperationException("'path' is zero-length.");
                }

                // folder...
                string folderPath = Path.GetDirectoryName(path);
                if (!(Directory.Exists(folderPath)))
                {
                    Directory.CreateDirectory(folderPath);
                }

                // mbr - 28-07-2006 - create new,.,.
                if ((flags & FileLoggerFlags.EnsureNewFile) != 0)
                {
                    path = Runtime.Current.GetUniqueFilePathInFolder(path);
                    if (path == null)
                    {
                        throw new InvalidOperationException("'path' is null.");
                    }
                    if (path.Length == 0)
                    {
                        throw new InvalidOperationException("'path' is zero-length.");
                    }
                }
                else
                {
                    // replace?
                    if ((flags & FileLoggerFlags.ReplaceExisting) != 0 && File.Exists(path))
                    {
                        try
                        {
                            File.Delete(path);
                        }
                        catch
                        {
                            // no-op the exception...
                        }
                    }
                }

                // formatter...
                if (formatter == null)
                {
                    formatter = FileLog.DefaultFormatter;
                }

                // mbr - 27-06-2007 - cleanup?  only on first use of the log, or if we have used it a lot.
                int count = (int)CleanupCount[name];
                if (count % (MaxLogFiles * 2) == 0)
                {
                    CleanupLogFolder(name, path);
                }

                // increment...
                CleanupCount[name] = count + 1;

                // create...
                FileLog log = new FileLog(name, path);
                log.Appenders.Add(new FileAppender(log, formatter, minLevel, maxLevel, path));

                // mbr - 28-11-2006 - screen?
                if ((int)(flags & FileLoggerFlags.EchoToDebug) != 0)
                {
                    log.Appenders.Add(new ConsoleAppender(log, formatter, minLevel, maxLevel));
                }

                // return...
                return(log);
            }
        }
Exemplo n.º 4
0
 /// <summary>
 /// Creates a file logger with the given name.
 /// </summary>
 /// <param name="name"></param>
 public static FileLog CreateFileLogger(string name, FileLoggerFlags flags)
 {
     // defer...
     return(CreateFileLogger(name, flags, null));
 }