예제 #1
0
            public TraceEventSink(string fileName, Func <TraceEvent, string> messageFormatter, Encoding encoding, bool autoFlush)
            {
                this.asyncFileWriter = new AsyncFileWriter(nameof(FileSink), fileName, encoding, autoFlush);

                this.messageFormatter = messageFormatter;

                this.onNext = OnNext;
            }
예제 #2
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;
                            }
                        }
                    }
                }
            }
예제 #3
0
            public StringSink(string fileName, Encoding encoding, bool autoFlush)
            {
                this.asyncFileWriter = new AsyncFileWriter(nameof(FileSink), fileName, encoding, autoFlush);

                this.onNext = OnNext;
            }