Advertisement
Guest User

Untitled

a guest
Mar 5th, 2015
189
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 10.14 KB | None | 0 0
  1. //------------------------------------------------------------
  2. // Copyright (c) Microsoft Corporation.  All rights reserved.
  3. //------------------------------------------------------------
  4.  
  5. using System;
  6. using System.Diagnostics;
  7. using System.Globalization;
  8. using System.IO;
  9.  
  10. namespace Microsoft.Samples.ServiceModel
  11. {
  12.  
  13.     public class CircularTraceListener : XmlWriterTraceListener
  14.     {
  15.  
  16.         static CircularStream m_stream = null;
  17.         bool MaxQuotaInitialized = false;
  18.         const string FileQuotaAttribute = "maxFileSizeKB";
  19.         const long DefaultMaxQuota = 1000;
  20.         const string DefaultTraceFile = "E2ETraces.svclog";
  21.  
  22.  
  23.         #region Member Functions
  24.  
  25.         private long MaxQuotaSize
  26.         {
  27.             //Get the MaxQuotaSize from configuration file
  28.             //Set to Default Value if there are any problems
  29.  
  30.             get
  31.             {
  32.                 long MaxFileQuota = 0;
  33.                 if (!this.MaxQuotaInitialized)
  34.                 {
  35.                     try
  36.                     {
  37.                         string MaxQuotaOption = this.Attributes[CircularTraceListener.FileQuotaAttribute];
  38.                         if (MaxQuotaOption == null)
  39.                         {
  40.                             MaxFileQuota = DefaultMaxQuota;
  41.                         }
  42.                         else
  43.                         {
  44.                             MaxFileQuota = int.Parse(MaxQuotaOption, CultureInfo.InvariantCulture);
  45.                         }
  46.                     }
  47.                     catch (Exception)
  48.                     {
  49.                         MaxFileQuota = DefaultMaxQuota;
  50.                     }
  51.                     finally
  52.                     {
  53.                         this.MaxQuotaInitialized = true;
  54.                     }
  55.                 }
  56.  
  57.                 if (MaxFileQuota <= 0)
  58.                 {
  59.                     MaxFileQuota = DefaultMaxQuota;
  60.                 }
  61.  
  62.                 //MaxFileQuota is in KB in the configuration file, convert to bytes
  63.  
  64.                 MaxFileQuota = MaxFileQuota * 1024;
  65.                 return MaxFileQuota;
  66.             }
  67.         }
  68.  
  69.         private void DetermineOverQuota()
  70.         {
  71.  
  72.             //Set the MaxQuota on the stream if it hasn't been done
  73.  
  74.             if (!this.MaxQuotaInitialized)
  75.             {
  76.                 m_stream.MaxQuotaSize = this.MaxQuotaSize;
  77.             }
  78.  
  79.             //If we're past the Quota, flush, then switch files
  80.  
  81.             if (m_stream.IsOverQuota)
  82.             {
  83.                 base.Flush();
  84.                 m_stream.SwitchFiles();
  85.             }
  86.         }
  87.  
  88.         #endregion              
  89.  
  90.         #region XmlWriterTraceListener Functions
  91.  
  92.         public CircularTraceListener(string file)
  93.             : base(m_stream = new CircularStream(file))
  94.         {
  95.         }
  96.  
  97.         public CircularTraceListener()
  98.             : base(m_stream = new CircularStream(DefaultTraceFile))
  99.         {
  100.         }
  101.  
  102.         protected override string[] GetSupportedAttributes()
  103.         {
  104.             return new string[] { CircularTraceListener.FileQuotaAttribute };
  105.         }
  106.  
  107.         public override void TraceData(TraceEventCache eventCache, string source, TraceEventType eventType, int id, object data)
  108.         {
  109.             DetermineOverQuota();
  110.             base.TraceData(eventCache, source, eventType, id, data);
  111.         }
  112.  
  113.         public override void TraceEvent(TraceEventCache eventCache, string source, TraceEventType eventType, int id)
  114.         {
  115.             DetermineOverQuota();
  116.             base.TraceEvent(eventCache, source, eventType, id);
  117.         }
  118.  
  119.         public override void TraceData(TraceEventCache eventCache, string source, TraceEventType eventType, int id, params object[] data)
  120.         {
  121.             DetermineOverQuota();
  122.             base.TraceData(eventCache, source, eventType, id, data);
  123.         }
  124.  
  125.         public override void TraceEvent(TraceEventCache eventCache, string source, TraceEventType eventType, int id, string format, params object[] args)
  126.         {
  127.             DetermineOverQuota();
  128.             base.TraceEvent(eventCache, source, eventType, id, format, args);
  129.         }
  130.  
  131.         public override void TraceEvent(TraceEventCache eventCache, string source, TraceEventType eventType, int id, string message)
  132.         {
  133.             DetermineOverQuota();
  134.             base.TraceEvent(eventCache, source, eventType, id, message);
  135.         }
  136.  
  137.         public override void TraceTransfer(TraceEventCache eventCache, string source, int id, string message, Guid relatedActivityId)
  138.         {
  139.             DetermineOverQuota();
  140.             base.TraceTransfer(eventCache, source, id, message, relatedActivityId);
  141.  
  142.         }
  143.  
  144.         #endregion
  145.  
  146.     }
  147.  
  148.     public class CircularStream : System.IO.Stream
  149.     {
  150.         private FileStream[] FStream = null;
  151.         private String[] FPath = null;
  152.         private long DataWritten = 0;
  153.         private long FileQuota = 0;
  154.         private int CurrentFile = 0;
  155.         private string stringWritten = string.Empty;
  156.  
  157.  
  158.         public CircularStream(string FileName)
  159.         {
  160.             //Handle all exceptions within this class, since tracing shouldn't crash a service
  161.  
  162.             //Add 00 and 01 to FileNames and open streams
  163.  
  164.             try
  165.             {
  166.                 string filePath = Path.GetDirectoryName(FileName);
  167.                 string fileBase = Path.GetFileNameWithoutExtension(FileName);
  168.                 string fileExt = Path.GetExtension(FileName);
  169.  
  170.                 if (string.IsNullOrEmpty(filePath))
  171.                 {
  172.                     filePath = AppDomain.CurrentDomain.BaseDirectory;
  173.                 }
  174.  
  175.                 FPath = new String[2];
  176.                 FPath[0] = Path.Combine(filePath, fileBase + "00" + fileExt);
  177.                 FPath[1] = Path.Combine(filePath, fileBase + "01" + fileExt);
  178.  
  179.                 FStream = new FileStream[2];
  180.                 FStream[0] = new FileStream(FPath[0], FileMode.Create);
  181.             }
  182.             catch { }
  183.  
  184.         }
  185.  
  186.         public long MaxQuotaSize
  187.         {
  188.             get
  189.             {
  190.                 return FileQuota;
  191.             }
  192.             set
  193.             {
  194.                 FileQuota = value;
  195.             }
  196.         }
  197.  
  198.         public void SwitchFiles()
  199.         {
  200.             try
  201.             {
  202.                 //Close current file, open next file (deleting its contents)
  203.  
  204.                 DataWritten = 0;
  205.                 FStream[CurrentFile].Close();
  206.  
  207.                 CurrentFile = (CurrentFile + 1) % 2;
  208.  
  209.                 FStream[CurrentFile] = new FileStream(FPath[CurrentFile], FileMode.Create);
  210.             }
  211.             catch (Exception) { }
  212.         }
  213.  
  214.         public bool IsOverQuota
  215.         {
  216.             get
  217.             {
  218.                 return (DataWritten >= FileQuota);
  219.             }
  220.  
  221.         }
  222.  
  223.         public override bool CanRead
  224.         {
  225.             get
  226.             {
  227.                 try
  228.                 {
  229.                     return FStream[CurrentFile].CanRead;
  230.                 }
  231.                 catch (Exception)
  232.                 {
  233.                     return true;
  234.                 }
  235.             }
  236.         }
  237.  
  238.         public override bool CanSeek
  239.         {
  240.             get
  241.             {
  242.                 try
  243.                 {
  244.                     return FStream[CurrentFile].CanSeek;
  245.                 }
  246.                 catch (Exception)
  247.                 {
  248.                     return false;
  249.                 }
  250.             }
  251.         }
  252.  
  253.         public override long Length
  254.         {
  255.             get
  256.             {
  257.                 try
  258.                 {
  259.                     return FStream[CurrentFile].Length;
  260.                 }
  261.                 catch (Exception)
  262.                 {
  263.                     return -1;
  264.                 }
  265.             }
  266.         }
  267.  
  268.         public override long Position
  269.         {
  270.             get
  271.             {
  272.                 try
  273.                 {
  274.                     return FStream[CurrentFile].Position;
  275.                 }
  276.                 catch (Exception)
  277.                 {
  278.                     return -1;
  279.                 }
  280.             }
  281.             set
  282.             {
  283.                 try
  284.                 {
  285.                     FStream[CurrentFile].Position = Position;
  286.                 }
  287.                 catch (Exception) { }
  288.             }
  289.         }
  290.  
  291.         public override bool CanWrite
  292.         {
  293.             get
  294.             {
  295.                 try
  296.                 {
  297.                     return FStream[CurrentFile].CanWrite;
  298.                 }
  299.                 catch (Exception)
  300.                 {
  301.                     return true;
  302.                 }
  303.             }
  304.         }
  305.  
  306.         public override void Flush()
  307.         {
  308.             try
  309.             {
  310.                 FStream[CurrentFile].Flush();
  311.             }
  312.             catch (Exception) { }
  313.         }
  314.  
  315.         public override long Seek(long offset, SeekOrigin origin)
  316.         {
  317.             try
  318.             {
  319.                 return FStream[CurrentFile].Seek(offset, origin);
  320.             }
  321.             catch (Exception)
  322.             {
  323.                 return -1;
  324.             }
  325.         }
  326.  
  327.         public override void SetLength(long value)
  328.         {
  329.             try
  330.             {
  331.                 FStream[CurrentFile].SetLength(value);
  332.             }
  333.             catch (Exception) { }
  334.         }
  335.  
  336.         public override void Write(byte[] buffer, int offset, int count)
  337.         {
  338.             try
  339.             {
  340.                 //Write to current file
  341.  
  342.                 FStream[CurrentFile].Write(buffer, offset, count);
  343.                 DataWritten += count;
  344.  
  345.             }
  346.             catch (Exception){ }
  347.         }
  348.  
  349.         public override int Read(byte[] buffer, int offset, int count)
  350.         {
  351.             try
  352.             {
  353.                 return FStream[CurrentFile].Read(buffer, offset, count);
  354.             }
  355.             catch (Exception)
  356.             {
  357.                 return -1;
  358.             }
  359.         }
  360.  
  361.         public override void Close()
  362.         {
  363.             try
  364.             {
  365.                 FStream[CurrentFile].Close();
  366.             }
  367.             catch (Exception) { }
  368.         }
  369.  
  370.  
  371.     }
  372.  
  373. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement