Example #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);
        }
Example #2
0
        protected void ReloadFileLoggerOptions(FileLoggerSettings settings)
        {
            lock ( _fileSync ) {
                bool resetStream = _rollOver != settings.RollOver;
                _rollOver = settings.RollOver;
                if (settings.FileName != _fileBaseName)
                {
                    _fileBaseName = settings.FileName;
                    resetStream   = true;
                }

                if (resetStream && _file != null)
                {
                    Interlocked.Exchange(ref _file, null)?.Dispose();
                }
            }
            _settings = settings;
            ReloadLoggerOptions(settings);
        }
Example #3
0
        private bool CloseCurrent()
        {
            FileRollOver rollOver = _settings.RollOver;

            if (rollOver == FileRollOver.Size)
            {
                int maxSize = _settings.MaxFileSize;
                if (maxSize <= 0)
                {
                    maxSize = 1024;
                }
                if (_file.BaseStream.Length / (1024 * 1024) <= maxSize)
                {
                    return(false);
                }
            }
            Interlocked.Exchange(ref _file, null)?.Dispose();
            return(true);
        }
Example #4
0
        private void GetDateMetric(StringBuilder b, FileRollOver rollOver)
        {
            var dt    = DateTime.UtcNow;
            int year  = dt.Year;
            int month = dt.Month;
            int day   = dt.Day;
            int hour  = dt.Hour;

            b.Append(year);
            b.Append('-');
            if (month <= 9)
            {
                b.Append('0');
            }
            b.Append(month);
            if (rollOver >= FileRollOver.Daily)
            {
                b.Append('-');
                if (day <= 9)
                {
                    b.Append('0');
                }
                b.Append(day);
                if (rollOver >= FileRollOver.Hourly)
                {
                    b.Append('T');
                    if (hour <= 9)
                    {
                        b.Append('0');
                    }
                    b.Append(hour);
                }
            }
            switch (rollOver)
            {
            case FileRollOver.Monthly:
                _metricDue = new DateTime(year, month, 1, 0, 0, 0, DateTimeKind.Utc).AddMonths(1);
                break;

            case FileRollOver.Daily:
                _metricDue = new DateTime(year, month, day, 0, 0, 0, DateTimeKind.Utc).AddDays(1);
                break;

            case FileRollOver.Hourly:
                _metricDue = new DateTime(year, month, day, hour, 0, 0, DateTimeKind.Utc).AddHours(1);
                break;

            default:
                // ?? Minutes from now
                int minutes = dt.Minute;
                b.Append('-');
                if (minutes <= 9)
                {
                    b.Append('0');
                }
                b.Append(minutes);
                // size checking every 4 minutes
                _metricDue = dt.AddMinutes(4);
                break;
            }
        }