예제 #1
0
        private TextStreamWriter CreateWriter()
        {
            string fileName = _fileBaseName;

            if (string.IsNullOrEmpty(fileName))
            {
                fileName = Path.GetFullPath("log.log");
            }
            DirectoryInfo dinfo = new DirectoryInfo(Path.GetDirectoryName(fileName));
            string        name  = Path.GetFileNameWithoutExtension(fileName);

            if (!dinfo.Exists)
            {
                dinfo.Create();
            }
            FileStream stream = null;

            using (StringBuilderCache c = StringBuilderCache.Acquire()) {
                c.Builder.Append(Path.Combine(dinfo.FullName, name)).Append('-');
                string       ext      = Path.GetExtension(fileName);
                FileRollOver rollOver = _settings.RollOver;
                if (rollOver >= FileRollOver.Monthly &&
                    rollOver <= FileRollOver.Hourly)
                {
                    GetDateMetric(c, rollOver);
                }
                else if (rollOver == FileRollOver.Size)
                {
                    stream = GetCounterFile(c.Builder, dinfo, name, ext);
                }
                else
                {
                    _metricDue = DateTime.MaxValue;
                }
                c.Builder.Append(ext);
                fileName = c.ToString();
            }
            if (stream == null)
            {
                stream = new FileStream(fileName, FileMode.Append, FileAccess.Write, FileShare.Read);
            }
            //} catch ( IOException ) {
            //    fileName += Guid.NewGuid().ToString();
            //    stream = new FileStream( fileName, FileMode.Append, FileAccess.Write, FileShare.Read );
            //}
            var file = new TextStreamWriter(stream, Encoding.UTF8, 4096, true);

            using (StringBuilderCache c = StringBuilderCache.Acquire()) {
                c.Builder.Append("## ")
                .Append(stream.Length > 0 ? "Append" : "Created")
                .Append('[')
                .Append(DateTime.UtcNow.ToString("s", DateTimeFormatInfo.InvariantInfo))
                .Append(']')
                .Append(Environment.NewLine);
                file.Write(c);
            }
            return(file);
        }
예제 #2
0
        protected internal override void Write(StringBuilder b)
        {
            TextStreamWriter file = LockWriter();

            try {
                file.Write(b);
            } finally {
                Monitor.Exit(_fileSync);
            }
        }