public DiagnosticPipeWriter(string applicationName = null) { applicationName = applicationName ?? Path.GetFileNameWithoutExtension(Assembly.GetEntryAssembly().Location); string dir = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData), "LogPipe", applicationName); if (Directory.Exists(dir) == false) Directory.CreateDirectory(dir); this.lockContext = new object(); hooks = new List<IDiagnosticHook>(); buffer = new AutoFlushingBuffer<LogEvent>(100); buffer.IsFlushReEntrant = false; buffer.ThrowOnFlushError = false; buffer.ErrorOccurred += buffer_ErrorOccurred; Action<IEnumerable<LogEvent>> flushAction = new Action<IEnumerable<LogEvent>>((events) => { var logPath = Path.Combine(dir, DateTime.UtcNow.Ticks+".js"); lock (cleanupTimer) { File.WriteAllText(logPath,JsonConvert.SerializeObject(events, Formatting.Indented)); } }); cleanupTimer = new Timer((o) => { CleanupOldLogs(dir); }, null, 1000, 5000); buffer.FlushAll = flushAction; buffer.StartAutoFlushing(1000); }
public DiagnosticPipeReader(string appName) { dir = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData), "LogPipe", appName); cutoff = new DateTime(2000, 1, 1); t = new Timer((o) => { Read(); }, null, 250, 250); bufferedProcesser = new AutoFlushingBuffer<BatchRecord>(100); bufferedProcesser.FlushAll = new Action<IEnumerable<BatchRecord>>((records) => { foreach (var record in records) { if (record.Timestamp > cutoff) { var text = File.ReadAllText(record.FullPath); List<LogEvent> events = JsonConvert.DeserializeObject<List<LogEvent>>(text); if (EventOccurred != null) EventOccurred(events); cutoff = record.Timestamp; } } }); bufferedProcesser.StartAutoFlushing(250); }