Click here to Skip to main content
15,889,874 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Here is Service Call,
C#
public void CalculateTimeTake(string isoCountryCode)
{
    try
    {
        DateTime startTime = DateTime.Now;
        TimeCalculator.BuildMessage("Service Started", true, startTime);
        //Some Service based Logic
        TimeCalculator.BuildMessage("Service Ended", false, startTime);

        TimeCalculator.DisplayMessage(); //To write a string to particular Event source
    }
    catch (Exception exception)
    {
        
    }
}

Here is the logic available in TimeCalculator,
C#
public class TimeCalculator
{
    private static StringBuilder sb = new StringBuilder();

    public static void BuildMessage(string MethodName, bool aboutToCall, DateTime startTime)
    {
        try
        {
            if (aboutToCall)
                sb.Append(("Calling Method: " + MethodName).PadRight(50, ' ') + "---> Time Recorded: " + startTime.ToString("MM/dd/yyyy HH:mm:ss:ffff", CultureInfo.InvariantCulture) + "; \n");
            else
                sb.Append(("Finished Method: " + MethodName).PadRight(50, ' ') + "---> Time Recorded: " <pre lang="c#">+ DateTime.Now.ToString("MM/dd/yyyy HH:mm:ss:ffff", CultureInfo.InvariantCulture) + ", Total time taken: " + string.Format("{0:n}", ((DateTime.Now - startTime).TotalMilliseconds).ToString()) + " ms ; \n");
        }
        catch (Exception ex)
        {
            Logger.Write("Exception Occured in Time Calculation.\nExceptionDetails:\n" + ex.ToString(), Util.Logging.LogLevel.Debug);
        }
    }

    public static void DisplayMessage()
    {
        try
        {
            Logger.Write(sb.ToString(), Util.Logging.LogLevel.Debug);
            sb = new StringBuilder();
        }
        catch (Exception ex)
        {
            Logger.Write(ex.ToString(), Util.Logging.LogLevel.Warning);
            sb = new StringBuilder();
        }
    }

The problem here is,

Suppose the
first service call is started at 12.10 AM, and has finished at 12.13 AM.
Second service call started at 12.11 AM, and has finished at 12.12 AM.

The string text of second call is overriding the first call string :(
I hope its related to the Static objects used, But can any one please help me how to rectify?
Posted
Updated 15-Oct-15 23:49pm
v3
Comments
Tomas Takac 16-Oct-15 5:51am    
Why don't you write directly to the log? Why to use the string builder at all?

1 solution

There is only a single instance of static variables across the app domain so all calls see the same string builder. You mentioned services but you haven't really explained how this code is called. If you are looking to time how long a call takes then just don't use static variables....create an instance of StringBuilder in your Calculate method and pass that to the BuildMessage and DisplayMessage methods so that they all access the same instance.

C#
public void CalculateTimeTake(string isoCountryCode)
        {
            try
            {
                StringBuild sb = new StringBuilder();
                DateTime startTime = DateTime.Now;
                TimeCalculator.BuildMessage(sb, "Service Started", true, startTime);
                //Some Service based Logic
                TimeCalculator.BuildMessage(sb, "Service Ended", false, startTime);

                TimeCalculator.DisplayMessage(sb); //To write a string to particular Event source
            }
            catch (Exception exception)
            {

            }


Update the other methods to use the StringBuilder you pass in rather than the static version.
 
Share this answer
 
Comments
Ravikumar Chunduri 16-Oct-15 6:51am    
Thanks for the answer, but the thing why am using it as a static string is because I will use the same Static variable initialized in Service project to my Business logic project and Data access project. And finally i will write the whole text to a location. So this is the reason why i have made static in the Time calculator function. Can you advice me now?
F-ES Sitecore 16-Oct-15 7:53am    
You should do the timing at the root level, or the point at which the code is started and not worry about the various child methods adding to the log, that's what I'd do anyway. If child tasks want to log their own times then they can do that additionally.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900