Exemplo n.º 1
0
    //public void WriteAllLines(string[] DetailItms)
    //{

    //	try
    //	{
    //		if (NoLog)
    //		{
    //			return;
    //		}

    //		foreach (string lin in DetailItms)
    //		{
    //			if (lin != null)
    //			{
    //				// Create the entry and push to the Queue
    //				ClsLogDetailItem DetailItm = new ClsLogDetailItem();

    //				LogWriteCount = LogWriteCount + 1;

    //				DetailItm.Idx = LogWriteCount;
    //				DetailItm.TimeUTC = DateTime.UtcNow;
    //				DetailItm.Message = lin;
    //				logQueue.Enqueue(DetailItm);
    //				if (LogWriteCount == long.MaxValue - 8)
    //				{
    //					LogWriteCount = 1;
    //				}
    //			}
    //		}

    //		//// If we have reached the Queue Size then flush the Queue
    //		//if (Flush)
    //		//{
    //		//	//Await FlushLog()
    //		//	FlushRightAway = true;
    //		//}
    //	}
    //	catch (Exception ex)
    //	{
    //		Console.WriteLine("Error: " + ex.Message);
    //	}
    //	finally
    //	{

    //	}
    //}
    public void WriteToLog(ClsLogDetailItem DetailItm, bool Flush = false)
    {
        try
        {
            if (this.NoLog)
            {
                return;
            }
            // Create the entry and push to the Queue
            this.LogWriteCount = this.LogWriteCount + 1;

            DetailItm.Idx     = this.LogWriteCount;
            DetailItm.TimeUTC = DateTime.UtcNow;
            this.logQueue.Enqueue(DetailItm);
            if (this.LogWriteCount == long.MaxValue - 8)
            {
                this.LogWriteCount = 1;
            }
            // If we have reached the Queue Size then flush the Queue
            if (this.LogWriteCount == 1 || Flush || this.logQueue.Count >= this.MaxLogQueueSize) //OrElse DoPeriodicFlush() Then
            {
                //Await FlushLog()
                this.FlushRightAway = true;
            }
        }
        catch (Exception ex)
        {
            Console.WriteLine("Error: " + ex.Message);
        }
        finally
        {
        }
    }
Exemplo n.º 2
0
 /// <summary>
 /// The single instance method that writes to the log file
 /// </summary>
 /// <param name="message">The message to write to the log</param>
 public void WriteToLog(string message, bool Flush = false)
 {
     try
     {
         if (this.NoLog)
         {
             return;
         }
         ClsLogDetailItem DI = new ClsLogDetailItem();
         DI.Message = message;
         this.WriteToLog(DI, Flush);
     }
     catch (Exception ex)
     {
         Console.WriteLine("Error: " + ex.Message);
     }
     finally
     {
     }
 }
Exemplo n.º 3
0
    //public virtual async Task<bool> ReadFile()
    //{
    //	long position = 0;
    //	try
    //	{
    //		return await this.ReadFile(position);
    //	}
    //	catch (Exception exception1)
    //	{
    //		return false;
    //	}
    //}

    //public virtual async Task<bool> ReadFile(long position)
    //{
    //	this.m_strings = new List<string>();
    //	if (!(await FileExistsAsync(this.m_path)))
    //	{
    //		return false;
    //	}
    //	FileStream stream = null;
    //	try
    //	{
    //		stream = new FileStream(this.m_path, FileMode.Open, FileAccess.Read, (FileShare.Delete | FileShare.ReadWrite));
    //	}
    //	catch (Exception obj1)
    //	{
    //		return false;
    //	}
    //	StreamReader reader = null;
    //	try
    //	{
    //		stream.Seek(position, SeekOrigin.Begin);
    //		if (position == 0)
    //		{
    //			reader = new StreamReader(stream, true);
    //		}
    //		else
    //		{
    //			reader = new StreamReader(stream, this.m_encoding);
    //		}
    //	}
    //	catch (Exception obj2)
    //	{
    //		stream.Close();
    //		return false;
    //	}
    //	bool flag = false;
    //	string item = string.Empty;
    //	try
    //	{
    //		do
    //		{
    //			item = await reader.ReadLineAsync();
    //			if (item != null)
    //			{
    //				this.m_strings.Add(item);
    //			}
    //			else
    //			{
    //				break;
    //			}
    //		} while (true);
    //		if (position == 0)
    //		{
    //			this.m_encoding = reader.CurrentEncoding;
    //		}
    //		position = stream.Position;
    //		flag = true;
    //	}
    //	catch (Exception exception1)
    //	{
    //	}
    //	reader.Close();
    //	stream.Close();
    //	return flag;
    //}

    public async Task <bool> WriteFile()
    {
        bool      Ret = false;
        Stopwatch SW  = new Stopwatch();

        SW.Start();
        try
        {
            bool Locked = await this.LockFile(FileShare.Read, FileSystemRights.CreateFiles& FileSystemRights.Write, FileAccess.Write, FileMode.OpenOrCreate);

            if (Locked)
            {
                StreamWriter writer = null;
                bool         Err    = false;
                try
                {
                    writer = new StreamWriter(this.m_fileStream, this.m_encoding); //With {.AutoFlush = True}
                }
                catch (Exception obj2)
                {
                    Console.WriteLine("Error creating streamwriter: " + obj2.Message);
                    if (writer != null)
                    {
                        writer.Close();
                    }
                    this.m_fileStream.Close();
                    Err = true;
                }
                if (Err == true)
                {
                    return(Ret);
                }
                long position = 0;
                try
                {
                    position = this.m_fileStream.Length;
                    this.m_fileStream.Seek(0, SeekOrigin.End);
                    ClsLogDetailItem OutQueueItem = new ClsLogDetailItem();

                    //if (position == 0)
                    //{
                    //	//Convert the class to tab delimited format - HEADER:
                    //	string OutCsv = null;
                    //	if (this.m_ProcessAsCSV)
                    //	{
                    //		OutCsv = ClsToCSV(OutQueueItem, true);
                    //		//write header
                    //		await writer.WriteLineAsync(OutCsv);
                    //	}
                    //	else
                    //	{
                    //		if (OutQueueItem.Message.Length > 0) //for some reason we are getting an empty line?
                    //		{
                    //			OutCsv = OutQueueItem.Message;
                    //			//write header
                    //			await writer.WriteLineAsync(OutCsv);
                    //		}
                    //	}
                    //}

                    while (this.WriteQueue.TryDequeue(out OutQueueItem))
                    {
                        //update time in queue
                        OutQueueItem.QueueWaitMS = (DateTime.UtcNow - OutQueueItem.TimeUTC).TotalMilliseconds;
                        //Convert the class to tab delimited format:
                        string OutCsv = null;
                        //if (this.m_ProcessAsCSV)
                        //{
                        //	OutCsv = ClsToCSV(OutQueueItem, false);
                        //}
                        //else
                        //{
                        OutCsv = OutQueueItem.Message;
                        //}
                        await writer.WriteLineAsync(OutCsv);
                    }
                    await writer.FlushAsync();

                    Ret = true;
                }
                catch (Exception exception1)
                {
                    Console.WriteLine("Error writing to streamwriter: " + exception1.Message);
                }
                try
                {
                    if (position > 0 && this.m_WasLocked)
                    {
                        this.m_fileStream.Unlock(position, 0xFFFF);
                    }
                }
                catch (IOException exception)
                {
                    Ret = false;
                    Console.WriteLine("Error unlocking file: " + exception.Message); //already unlocked
                }

                writer.Close();
            }
            else
            {
                Ret = false;
            }
        }
        catch (Exception ex)
        {
            Console.WriteLine("Error: " + ex.Message);
        }
        finally
        {
            if (Ret)
            {
                this.LastLogWriteTime = DateTime.Now;
            }
        }

        return(Ret);
    }