-1

I am trying to write data into a json file using C# and JSON.NET referring the accepted answer suggested here (How to write a JSON file in C#?). When I run the code below I am not getting any error message but also the data is not written to the json file.

Functionally what I am trying to achieve is creating a json file that stores times for actions that are done while executing Nunit test

I have tried to implement the solution suggested here (Deserializing JSON data to C# using JSON.NET) but it did not solve my problem

public static class ActionTimeHelper
{

private static readonly string _actionTimeLogFileName = "ActionTimeLog_" + string.Format("{0:yyyy_MM_dd_hhmmss}", DateTime.Now);
[ThreadStatic] private static FileStream _fileStream = null;
[ThreadStatic] private static StreamWriter _actionStreamWriter = null;
[ThreadStatic] private static JsonWriter _jsonWriter = null;
[ThreadStatic] private static List<ActionTimeInfo> actionList = new List<ActionTimeInfo>();

public static void CreateActionTimeLogFile(string logPath, string testName)
{
    string dir = logPath + testName + @"\";
    if (!Directory.Exists(dir))
    {
        Directory.CreateDirectory(dir);
    }
    _fileStream = File.Open(dir + _actionTimeLogFileName + ".json", FileMode.CreateNew);
    _actionStreamWriter = new StreamWriter(_fileStream);
    _jsonWriter = new JsonTextWriter(_actionStreamWriter);
    _jsonWriter.Formatting = Formatting.Indented;
    JsonSerializer serializer = new JsonSerializer();
    serializer.Serialize(_jsonWriter, actionList.ToArray().ToString());
    //var jarray = JsonConvert.DeserializeObject<List<ActionTimeInfo>>(actionList.ToArray().ToString());

}

public static void StartActionTime(string actionName)
{
    actionList.Add(new ActionTimeInfo()
    {
        ActionName = actionName,
        StartTime = DateTime.Now
    });
}

public static void EndActionTime(string actionName)
{
    ActionTimeInfo endAction = actionList.Find(actionInfo => actionInfo.ActionName.Equals(actionName));
    endAction.EndTime = DateTime.Now;
    endAction.ExecutionTime = endAction.EndTime.Subtract(endAction.StartTime);
}

}

public class ActionTimeInfo
{
public string ActionName { get; set; }

public DateTime StartTime { get; set; }

public DateTime EndTime { get; set; }

public TimeSpan ExecutionTime { get; set; }

}

Usage of above classes in UUnit test:

[Test, Parallelizable, RequiresThread]
public void TestMethod3()
{
    ActionTimeHelper.StartActionTime("Login Action");
    ActionTimeHelper.EndActionTime("Login Action");
}

[TearDown]
public void TestTearDown()
{
    ActionTimeHelper.CreateActionTimeLogFile(TCRunSettings.LogPath, TestContext.CurrentContext.Test.Name);
}

Actual Result: JSON file created in the directory but no list items written to it Expected Result: JSON file created in the directory along with list items written to it

test_user
  • 69
  • 1
  • 7
  • suspicion the stream isn't flushed.... try wrapping your FileStream in a using so it gets disposed correctly (i.e. the dispose will flush the stream) although you seem to have delared it as a static field, and disposing static fields can be problematic... so try a manual dispose/flush – OJay Apr 26 '19 at 02:58
  • @OJay : Thanks flushing helped solve the problem. – test_user Apr 26 '19 at 03:05
  • Can someone please explain the downvote. I will work on improving myself. – test_user Apr 26 '19 at 04:05

1 Answers1

0

I think you're missing flush and close for _jsonWriter?

maybe try

_jsonWriter.Flush();
_jsonWriter.Close();

at the end of the method

or even better, use using to wrap the _fileStream, _actionStreamWriter and _jsonWriter

Ted Zhang
  • 341
  • 2
  • 6