using System; using System.IO; using Newtonsoft.Json; using Newtonsoft.Json.Serialization; namespace Semmle.Util { /// /// A wrapper around an underlying which allows /// objects to be serialized to it. /// public sealed class DiagnosticsStream : IDiagnosticsWriter { private readonly JsonSerializer serializer; private readonly StreamWriter writer; /// /// Initialises a new for a file at . /// /// The path to the file that should be created. public DiagnosticsStream(string path) { this.writer = File.CreateText(path); var contractResolver = new DefaultContractResolver { NamingStrategy = new CamelCaseNamingStrategy() }; serializer = new JsonSerializer { ContractResolver = contractResolver, NullValueHandling = NullValueHandling.Ignore }; } /// /// Adds as a new diagnostics entry. /// /// The diagnostics entry to add. public void AddEntry(DiagnosticMessage message) { serializer.Serialize(writer, message); writer.Flush(); } /// /// Releases all resources used by the object. /// public void Dispose() { writer.Dispose(); } } }