Exemple #1
0
        public void AcquireTimeOut()
        {
            _writer.WriteLine("Acquire Lock 1");
            var lock1 = LockFile.Acquire("test.lock");

            _writer.WriteLine("Acquire Lock 2");
            Assert.Throws <TimeoutException>(() => LockFile.Acquire("test.lock", TimeSpan.FromSeconds(1)));
        }
Exemple #2
0
        public void AquireTimeOut()
        {
            Console.WriteLine("Acquire Lock 1");
            var lock1 = LockFile.Acquire("test.lock");

            Console.WriteLine("Acquire Lock 2");
            var lock2 = LockFile.Acquire("test.lock", TimeSpan.FromSeconds(1));
        }
        public void Flush()
        {
            if (_isFlushing || _buffer.IsEmpty)
            {
                return;
            }

            // Only check if appending file and size hasn't been checked in 2 minutes
            if ((_append || !_firstWrite) && DateTime.UtcNow.Subtract(_lastSizeCheck).TotalSeconds > 120)
            {
                CheckFileSize();
            }

            if (_isFlushing || _buffer.IsEmpty)
            {
                return;
            }

            try {
                _isFlushing = true;

                Run.WithRetries(() => {
                    using (var logFileLock = LockFile.Acquire(FilePath + ".lock", TimeSpan.FromMilliseconds(500))) {
                        bool append = _append || !_firstWrite;
                        _firstWrite = false;

                        try {
                            using (var writer = GetWriter(append)) {
                                LogEntry entry;
                                while (_buffer.TryDequeue(out entry))
                                {
                                    if (entry != null && entry.LogLevel >= MinimumLogLevel)
                                    {
                                        writer.Value.WriteLine($"{FormatLongDate(entry.Timestamp)} {entry.LogLevel.ToString().PadRight(5)} {entry.Message}");
                                    }
                                }
                            }
                        } catch (Exception ex) {
                            System.Diagnostics.Trace.TraceError("Unable flush the logs. " + ex.Message);
                            LogEntry entry;
                            while (_buffer.TryDequeue(out entry))
                            {
                                System.Diagnostics.Trace.WriteLine(entry);
                            }
                        }
                    }
                });
            } catch (Exception ex) {
                System.Diagnostics.Trace.WriteLine("Exceptionless: Error flushing log contents to disk: {0}", ex.Message);
            } finally {
                _isFlushing = false;
            }
        }
        internal void CheckFileSize()
        {
            if (_isCheckingFileSize)
            {
                return;
            }

            _isCheckingFileSize = true;
            _lastSizeCheck      = DateTime.UtcNow;

            if (GetFileSize() <= FIVE_MB)
            {
                _isCheckingFileSize = false;
                return;
            }

            // get the last X lines from the current file
            string lastLines = String.Empty;

            try {
                Run.WithRetries(() => {
                    using (var logFileLock = LockFile.Acquire(FilePath + ".lock", TimeSpan.FromMilliseconds(500))) {
                        try {
                            lastLines = GetLastLinesFromFile(FilePath);
                        } catch {}
                    }
                });
            } catch (Exception ex) {
                System.Diagnostics.Trace.WriteLine("Exceptionless: Error getting last X lines from the log file: {0}", ex.Message);
            }

            if (String.IsNullOrEmpty(lastLines))
            {
                _isCheckingFileSize = false;
                return;
            }

            // overwrite the log file and initialize it with the last X lines it had
            try {
                Run.WithRetries(() => {
                    using (var logFileLock = LockFile.Acquire(FilePath + ".lock", TimeSpan.FromMilliseconds(500))) {
                        using (var writer = GetWriter(true))
                            writer.Value.Write(lastLines);
                    }
                });
            } catch (Exception ex) {
                System.Diagnostics.Trace.WriteLine("Exceptionless: Error rewriting the log file after trimming it: {0}", ex.Message);
            }

            _isCheckingFileSize = false;
        }
        public void Flush()
        {
            if (_isFlushing || _buffer.Count == 0)
            {
                return;
            }

            if (DateTime.Now.Subtract(_lastSizeCheck).TotalSeconds > 120)
            {
                CheckFileSize();
            }

            try {
                _isFlushing = true;

                Run.WithRetries(() => {
                    using (var logFileLock = LockFile.Acquire(FilePath + ".lock", TimeSpan.FromMilliseconds(500))) {
                        bool append = _append || !_firstWrite;
                        _firstWrite = false;

                        try {
                            using (var writer = GetWriter(append)) {
                                LogEntry entry;
                                while (_buffer.TryDequeue(out entry))
                                {
                                    if (entry != null && entry.LogLevel >= MinimumLogLevel)
                                    {
                                        writer.Value.WriteLine(entry.Message);
                                    }
                                }
                            }
                        } catch (Exception ex) {
                            System.Diagnostics.Trace.TraceError("Unable flush the logs. " + ex.Message);
                            LogEntry entry;
                            while (_buffer.TryDequeue(out entry))
                            {
                                System.Diagnostics.Trace.WriteLine(entry);
                            }
                        }
                    }
                });
            } catch (Exception ex) {
                System.Diagnostics.Trace.WriteLine("Exceptionless: Error flushing log contents to disk: {0}", ex.Message);
            } finally {
                _isFlushing = false;
            }
        }
Exemple #6
0
        public void Acquire()
        {
            var thread1 = new Thread(s => {
                _writer.WriteLine("[Thread: {0}] Lock 1 Entry", Thread.CurrentThread.ManagedThreadId);
                using (var lock1 = LockFile.Acquire("Acquire.lock")) {
                    _writer.WriteLine("[Thread: {0}] Lock 1", Thread.CurrentThread.ManagedThreadId);
                    Thread.Sleep(TimeSpan.FromSeconds(2));
                }
            });

            thread1.Start();

            var thread2 = new Thread(s => {
                _writer.WriteLine("[Thread: {0}] Lock 2 Entry", Thread.CurrentThread.ManagedThreadId);
                using (var lock2 = LockFile.Acquire("Acquire.lock")) {
                    _writer.WriteLine("[Thread: {0}] Lock 2", Thread.CurrentThread.ManagedThreadId);
                }
            });

            thread2.Start();
            Thread.Sleep(TimeSpan.FromSeconds(5));
        }