Beispiel #1
0
            void OnNextCore(T value)
            {
                string v;

                try
                {
                    v = messageFormatter(value);
                }
                catch (Exception ex)
                {
                    EtwStreamEventSource.Log.SinkError(nameof(RollingFileSink), "messageFormatter convert failed", ex.ToString());
                    return;
                }
                asyncFileWriter.Enqueue(v);
            }
Beispiel #2
0
            public override void OnNext(EventWrittenEventArgs value)
            {
                string v;

                try
                {
                    v = messageFormatter(value);
                }
                catch (Exception ex)
                {
                    EtwStreamEventSource.Log.SinkError(nameof(FileSink), "messageFormatter convert failed", ex.ToString());
                    return;
                }

                asyncFileWriter.Enqueue(v);
            }
Beispiel #3
0
            protected void CheckFileRolling()
            {
                var    now = DateTime.Now;
                string ts;

                try
                {
                    ts = timestampPattern(now);
                }
                catch (Exception ex)
                {
                    EtwStreamEventSource.Log.SinkError(nameof(RollingFileSink), "timestampPattern convert failed", ex.ToString());
                    return;
                }

                // needs to create next file
                var disposeTarget = asyncFileWriter;

                if (disposeTarget == null || ts != currentTimestampPattern || disposeTarget?.CurrentStreamLength >= rollSizeInBytes)
                {
                    lock (newFileLock)
                    {
                        if (this.asyncFileWriter == disposeTarget)
                        {
                            int sequenceNo = 0;
                            if (disposeTarget != null)
                            {
                                sequenceNo = ExtractCurrentSequence(asyncFileWriter.FileName) + 1;
                            }

                            string fn = null;
                            while (true)
                            {
                                try
                                {
                                    var newFn = fileNameSelector(now, sequenceNo);
                                    if (fn == newFn)
                                    {
                                        EtwStreamEventSource.Log.SinkError(nameof(RollingFileSink), "fileNameSelector indicate same filname", "");
                                        return;
                                    }
                                    fn = newFn;
                                }
                                catch (Exception ex)
                                {
                                    EtwStreamEventSource.Log.SinkError(nameof(RollingFileSink), "fileNamemessageFormatter convert failed", ex.ToString());
                                    return;
                                }

                                var fi = new FileInfo(fn);
                                if (fi.Exists)
                                {
                                    if (fi.Length >= rollSizeInBytes)
                                    {
                                        sequenceNo++;
                                        continue;
                                    }
                                }
                                break;
                            }

                            string[] safe;
                            try
                            {
                                safe = disposeTarget?.Finalize(); // block!
                            }
                            catch (Exception ex)
                            {
                                EtwStreamEventSource.Log.SinkError(nameof(RollingFileSink), "Can't dispose fileStream", ex.ToString());
                                return;
                            }
                            try
                            {
                                asyncFileWriter         = new AsyncFileWriter(nameof(RollingFileSink), fn, encoding, autoFlush);
                                currentTimestampPattern = ts;
                                if (safe != null)
                                {
                                    foreach (var item in safe)
                                    {
                                        asyncFileWriter.Enqueue(item);
                                    }
                                }
                            }
                            catch (Exception ex)
                            {
                                EtwStreamEventSource.Log.SinkError(nameof(RollingFileSink), "Can't create FileStream", ex.ToString());
                                return;
                            }
                        }
                    }
                }
            }
Beispiel #4
0
 public override void OnNext(string value)
 {
     asyncFileWriter.Enqueue(value);
 }