Is there a way to wait when listener is finised writing to file. Right now half of the data is cut off. I think its becouse TextWriterTraceListener dosent manage to write data to file before the application closes.
static void Main(string[] args)
{
Stopwatch stopwatch = new Stopwatch();
// Begin timing
stopwatch.Start();
try
{
Trace.Listeners.Add(new ConsoleTraceListener(false));
Trace.WriteLine(" ");
Trace.WriteLine(DateTime.Now);
Trace.WriteLine("Starting application...");
Trace.WriteLine("-----------------------------------------------------------");
Trace.WriteLine("Elapsed ms | Task information");
Trace.WriteLine("-----------------------------------------------------------");
Console.ForegroundColor = ConsoleColor.Green;
foreach (int i in Enumerable.Range(1, 3))
{
Trace.WriteLine("Keypair nr: " + i);
Trace.WriteLine(stopwatch.ElapsedMilliseconds + "ms | Starting to generate bytes for wrapping key..");
byte[] keyBytes = generateKeyBytes();
Trace.WriteLine(stopwatch.ElapsedMilliseconds + "ms | ..generation finished.");
Trace.WriteLine(stopwatch.ElapsedMilliseconds + "ms | Executing java to generate private and public keys..");
string keysString = runJava(keyBytes);
Trace.WriteLine(stopwatch.ElapsedMilliseconds + "ms | ..generation finished.");
string[] keys = keysString.Split(',');
byte[] pubKey = Convert.FromBase64String(keys[0]);
byte[] wrappedKey = Convert.FromBase64String(keys[1]);
Trace.WriteLine(stopwatch.ElapsedMilliseconds + "ms | Decrypting the private key..");
byte[] privKey = Decrypt(wrappedKey, keyBytes);
Trace.WriteLine(stopwatch.ElapsedMilliseconds + "ms | ..decryption finished.");
Trace.WriteLine("Public Key:\n\r" + BitConverter.ToString(pubKey));
Trace.WriteLine("Private Key:\n\r" + BitConverter.ToString(privKey));
}
}
catch (Exception ex)
{
Console.ForegroundColor = ConsoleColor.Red;
Trace.WriteLine("From: " + ex.Source + ".\nDetail: "+ ex.Message);
}
finally
{
Trace.WriteLine("Total Elapsed time: " + stopwatch.Elapsed);
stopwatch.Stop();
Console.WriteLine("Any key to exit..");
Console.ReadKey();
}
}
Added my code.
And in config file i have:
<system.diagnostics>
<trace autoflush="false" indentsize="4">
<listeners>
<add name="myListener"
type="System.Diagnostics.TextWriterTraceListener"
initializeData="output.log" />
<remove name="Default" />
</listeners>
</trace>
</system.diagnostics>
The last byte array what is printed out is cut off and the lines from finnaly block are allso missing from the output.log but still they appear on the console.
The Trace and Debug classes have Flush, Close and AutoFlush members. You need to use at least one of them. That the ConsoleListener works without them is accidental.