Ejemplo n.º 1
0
 public static void CheckFile(TraceListener listener, RollingWriterOptions options)
 {
     lock (listener)
     {
         // If first time then...
         if (!options.SettingsLoaded)
         {
             options.Load(listener.Attributes);
             var flags = System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Instance;
             // .NET Framework
             options._WriterField = listener.GetType().GetField("writer", flags);
             // .NET Core.
             if (options._WriterField == null)
             {
                 options._WriterField = listener.GetType().GetField("_writer", flags);
             }
             options.SettingsLoaded = true;
         }
         var dir          = Path.GetDirectoryName(options._FileNameBasic);
         var nam          = Path.GetFileNameWithoutExtension(options._FileNameBasic);
         var ext          = Path.GetExtension(options._FileNameBasic);
         var pathPattern  = Path.Combine(dir, nam + options.SuffixPattern + ext);
         var expandedPath = Configuration.AssemblyInfo.ExpandPath(pathPattern);
         var path         = string.Format(expandedPath, DateTime.Now);
         var writer       = (StreamWriter)options._WriterField.GetValue(listener);
         // If file is missing or name changed then...
         if (writer == null || !path.Equals(((FileStream)writer.BaseStream).Name, StringComparison.OrdinalIgnoreCase))
         {
             if (writer != null)
             {
                 writer.Close();
             }
             // Cleanup old files.
             // Get wipe conditions.
             WipeOldLogFiles(expandedPath, options.LogFileMaxFiles, options.LogFileMaxBytes);
             var fi = new FileInfo(path);
             if (!fi.Directory.Exists)
             {
                 fi.Directory.Create();
             }
             // Create a new file stream and a new stream writer and pass it to the listener.
             var stream = new FileStream(path, FileMode.OpenOrCreate);
             writer = new StreamWriter(stream);
             stream.Seek(0, SeekOrigin.End);
             options._WriterField.SetValue(listener, writer);
         }
     }
 }
Ejemplo n.º 2
0
 public override void TraceEvent(TraceEventCache eventCache, string source, TraceEventType eventType, int id, string message)
 {
     RollingWriterOptions.CheckFile(this, Options);
     base.TraceEvent(eventCache, source, eventType, id, message);
 }
Ejemplo n.º 3
0
 public override void TraceData(TraceEventCache eventCache, string source, TraceEventType eventType, int id, params object[] data)
 {
     RollingWriterOptions.CheckFile(this, Options);
     base.TraceData(eventCache, source, eventType, id, data);
 }
Ejemplo n.º 4
0
 public override void Fail(string message, string detailMessage)
 {
     RollingWriterOptions.CheckFile(this, Options);
     base.Fail(message, detailMessage);
 }
Ejemplo n.º 5
0
 /// <summary>
 /// Gets the custom attributes supported by the trace listener.
 /// </summary>
 /// <returns>
 /// A string array naming the custom attributes supported by the trace listener, or null if there are no custom attributes.
 /// </returns>
 protected override string[] GetSupportedAttributes()
 => RollingWriterOptions.GetSupportedAttributes();
Ejemplo n.º 6
0
 /// <summary>
 /// Initializes a new instance of the <see cref="RollingXmlWriterTraceListener"/> class by specifying the trace file
 /// name and the name of the new instance.
 /// </summary>
 /// <param name="filename">The trace file name.</param>
 /// <param name="name">The name of the new instance.</param>
 public RollingXmlWriterTraceListener(string filename, string name)
     : base(filename, name)
 {
     Options = new RollingWriterOptions(filename);
 }
Ejemplo n.º 7
0
 public override void TraceTransfer(TraceEventCache eventCache, string source, int id, string message, Guid relatedActivityId)
 {
     RollingWriterOptions.CheckFile(this, Options);
     base.TraceTransfer(eventCache, source, id, message, relatedActivityId);
 }
Ejemplo n.º 8
0
 public override void TraceEvent(TraceEventCache eventCache, string source, TraceEventType eventType, int id, string format, params object[] args)
 {
     RollingWriterOptions.CheckFile(this, Options);
     base.TraceEvent(eventCache, source, eventType, id, format, args);
 }