0

My counter is saved on a text file. My problem is that the counter restarts at zero without my knowledge. I only experienced this once for now. But I want to know why does the code restart the counter. Here is the code:

public class HitCounterAttribute : ActionFilterAttribute
{
    public override void OnActionExecuted(ActionExecutedContext filterContext)
    {
        base.OnActionExecuted(filterContext);

        string lastCount = "";
        try
        {
            StreamReader SR = File.OpenText(HttpContext.Current.Server.MapPath("~/Helpers/HitCounter.txt"));
            string getCount = null;
            while ((getCount = SR.ReadLine()) != null)
            {
                lastCount = lastCount + getCount;
            }
            SR.Close();
            long newCount = Convert.ToInt64(lastCount);
            newCount++;
            TextWriter TxtWtr = new StreamWriter(HttpContext.Current.Server.MapPath("~/Helpers/HitCounter.txt"));
            TxtWtr.WriteLine(Convert.ToString(newCount));
            TxtWtr.Close();
            SR = File.OpenText(HttpContext.Current.Server.MapPath("~/Helpers/HitCounter.txt"));
            getCount = null;
            lastCount = "";
            while ((getCount = SR.ReadLine()) != null)
            {
                lastCount = lastCount + getCount;
            }
            SR.Close();
        }
        catch (Exception ex)
        {
            TextWriter TxtWtr = new StreamWriter(HttpContext.Current.Server.MapPath("~/Helpers/HitCounter.txt"));
            TxtWtr.WriteLine(Convert.ToString("1"));
            TxtWtr.Close();
            lastCount = "1";
        }
    }
}

My question is that:

  1. Why do this code restart the counter to zero?
  2. What seems to be the reasons in doing such?
  3. Are there ways to tweak the code so that the counter will never restart again?

Thank you very much for your help. My knowledge about asp.net mvc is still very little so please bear with me.

EDIT:

This is the exception generated everytime the application restarts the counter to 1.

System.IO.IOException: The process cannot access the file '~\Helpers\HitCounter.txt' because it is being used by another process.

at System.IO.__Error.WinIOError(Int32 errorCode, String maybeFullPath)

at System.IO.FileStream.Init(String path, FileMode mode, FileAccess access, Int32 rights, Boolean useRights, FileShare share, Int32 bufferSize, FileOptions options, SECURITY_ATTRIBUTES secAttrs, String msgPath, Boolean bFromProxy, Boolean useLongPath)

at System.IO.FileStream..ctor(String path, FileMode mode, FileAccess access, FileShare share, Int32 bufferSize, FileOptions options)

at System.IO.StreamWriter.CreateFile(String path, Boolean append)

at System.IO.StreamWriter..ctor(String path, Boolean append, Encoding encoding, Int32 bufferSize)

at System.IO.StreamWriter..ctor(String path)

at Application.Helpers.HitCounterAttribute.OnActionExecuted(ActionExecutedContext filterContext) in ~\Helpers\HitCounterAttribute.cs:line 14

*cs:line 14 is where the code: base.OnActionExecuted(filterContext);

ALSO, I noticed that this happens everyday during the night. I wasn't able to catch the exact time since I was busy with something else. But I am sure that this happens everyday.

  • string lastCount should be a long and should intialize as 0 – Valamas Sep 20 '13 at 02:28
  • Ok. I managed to change all string types into a long. Also converted the StreamReader to an Int64. So far the application runs as it supposed to be. Will this change never restart the count to zero again? Thank you for the immediate reply. But I am still not sure why the application restarted the count to zero. My questions 1 and 2 hasn't been answered yet. Also, thank you for editing my post. – Pedro Penduko Sep 20 '13 at 02:53
  • You say `reset to 0`, however, if it actually resets to 1, then an exception is being thrown. You should at least append the exception to another file (log) and see what it is. – Valamas Sep 20 '13 at 03:13
  • I added this line unto the catch part of the code: TextWriter LogWtr = new StreamWriter(HttpContext.Current.Server.MapPath("~/Helpers/ErrorLog.txt")); LogWtr.WriteLine(ex); LogWtr.Close(); I just have to wait for my application to throw an exception and see what is in there. Thank you for your help. Will be back for updates. – Pedro Penduko Sep 20 '13 at 03:54

0 Answers0