EDIT:
My previous answer (below) addressed the question as stated. but as pointed out in a comment, it would not produce a valid json file. just valid json in each line of the file. the following would produce a valid json file with multiple entries:
List<object> log = new List<object>();
JsonSerializer serializer = new JsonSerializer();
string path = @"C:\Users\COCON\source\repos\DiscordAdmin\DiscordAdmin\Logs\Purge.json";
if (System.IO.File.Exists(path))
{
using (System.IO.StreamReader reader = new System.IO.StreamReader(path))
{
Newtonsoft.Json.JsonReader jreader = new Newtonsoft.Json.JsonTextReader(reader);
log = serializer.Deserialize<List<object>>(jreader);
}
}
using (System.IO.StreamWriter file =
new System.IO.StreamWriter(path, false))
{
object logEntry = LocalTime + ": " + Sender + " has executed the purge command on " + message + " messages";
log.Add(logEntry);
serializer.Serialize(file, log); //Serialize the time, the author of the command and how many messages they purged
}
Previous Answer: (produces valid json in each line, but not valid json file)
using (System.IO.StreamWriter file =
new System.IO.StreamWriter(@"C:\Users\COCON\source\repos\DiscordAdmin\DiscordAdmin\Logs\Purge.json", true))
{
JsonSerializer serializer = new JsonSerializer();
//serialize object directly into file stream
serializer.Serialize(file, LocalTime + ": " + Sender + " has executed the purge command on " + message + " messages"); //Serialize the time, the author of the command and how many messages they purged
}
the "true" value means to append, as opposed to creating a new file or overwriting. Seems CreateText does not come with that option.
from the File.CreateText documentation:
This method is equivalent to the StreamWriter(String, Boolean)
constructor overload with the append parameter set to false.
File.CreateText
StreamWriter (String, Boolean)
File.CreateTextwill create a new file and override any existing one. Do notice that if you do this you won't be able to magically deserialize the file, you'd have to do it manually line by line