Skip to content

Commit

Permalink
Added logging traces to a file.
Browse files Browse the repository at this point in the history
  • Loading branch information
dlebansais committed Jun 4, 2019
1 parent 4ca1d17 commit 1196db9
Show file tree
Hide file tree
Showing 4 changed files with 109 additions and 24 deletions.
2 changes: 2 additions & 0 deletions Kill-Update-Plugin/Kill-Update-Plugin.cs
Original file line number Diff line number Diff line change
Expand Up @@ -268,6 +268,8 @@ private void OnUpdate()

try
{
Logger.AddLog("Running timer callback");

ServiceStartMode? PreviousStartType = StartType;
bool LockIt = IsSettingLock;

Expand Down
4 changes: 2 additions & 2 deletions Kill-Update-Plugin/Properties/AssemblyInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
// You can specify all the values or you can default the Build and Revision Numbers
// by using the '*' as shown below:
// [assembly: AssemblyVersion("1.0.*")]
[assembly: AssemblyVersion("1.0.0.44")]
[assembly: AssemblyFileVersion("1.0.0.41")]
[assembly: AssemblyVersion("1.0.0.60")]
[assembly: AssemblyFileVersion("1.0.0.42")]
[assembly: NeutralResourcesLanguage("en-US")]

123 changes: 103 additions & 20 deletions SinglePluginHost/Plugin/PluginLogger.cs
Original file line number Diff line number Diff line change
@@ -1,59 +1,142 @@
using System;
using System.Globalization;
using System.IO;
using System.Reflection;
using System.Runtime.InteropServices;

namespace TaskbarIconHost
{
public class PluginLogger : IPluginLogger
{
public PluginLogger()
{
#if DEBUG
IsLogOn = true;
#endif

try
{
string Location = Assembly.GetExecutingAssembly().Location;
string SettingFilePath = Path.Combine(Path.GetDirectoryName(Location), "settings.txt");

if (File.Exists(SettingFilePath))
{
using (FileStream fs = new FileStream(SettingFilePath, FileMode.Open, FileAccess.Read))
{
using (StreamReader sr = new StreamReader(fs))
{
TraceFilePath = sr.ReadLine();
}
}
}

if (!string.IsNullOrEmpty(TraceFilePath))
{
bool IsFirstTraceWritten = false;

using (FileStream fs = new FileStream(TraceFilePath, FileMode.Append, FileAccess.Write))
{
using (StreamWriter sw = new StreamWriter(fs))
{
sw.WriteLine("** Log started **");
IsFirstTraceWritten = true;
}
}

if (IsFirstTraceWritten)
{
IsLogOn = true;
IsFileLogOn = true;
}
}
}
catch (Exception e)
{
PrintLine("Unable to start logging traces.");
PrintLine(e.Message);
}
}

public void AddLog(string text)
{
AddLog(text, false);
}

public void AddLog(string text, bool showNow)
{
#if DEBUG
lock (GlobalLock)
if (IsLogOn)
{
DateTime UtcNow = DateTime.UtcNow;
string TimeLog = UtcNow.ToString(CultureInfo.InvariantCulture) + UtcNow.Millisecond.ToString("D3");
lock (GlobalLock)
{
DateTime UtcNow = DateTime.UtcNow;
string TimeLog = UtcNow.ToString(CultureInfo.InvariantCulture) + UtcNow.Millisecond.ToString("D3");

string Line = $"TaskbarIconHost - {TimeLog}: {text}\n";
string Line = $"TaskbarIconHost - {TimeLog}: {text}\n";

if (LogLines == null)
LogLines = Line;
else
LogLines += Line;
if (LogLines == null)
LogLines = Line;
else
LogLines += Line;
}
}
#endif

if (showNow)
PrintLog();
}

public void PrintLog()
{
#if DEBUG
lock (GlobalLock)
if (IsLogOn)
{
if (LogLines != null)
lock (GlobalLock)
{
string[] Lines = LogLines.Split('\n');
foreach (string Line in Lines)
OutputDebugString(Line);
if (LogLines != null)
{
string[] Lines = LogLines.Split('\n');
foreach (string Line in Lines)
PrintLine(Line);

LogLines = null;
LogLines = null;
}
}
}
#endif
}

private void PrintLine(string line)
{
OutputDebugString(line);

if (IsFileLogOn)
WriteLineToTraceFile(line);
}

private void WriteLineToTraceFile(string line)
{
try
{
if (line.Length == 0)
return;

using (FileStream fs = new FileStream(TraceFilePath, FileMode.Append, FileAccess.Write))
{
using (StreamWriter sw = new StreamWriter(fs))
{
sw.WriteLine(line);
}
}
}
catch
{
}
}

[DllImport("kernel32", CharSet = CharSet.Unicode)]
public static extern void OutputDebugString([In][MarshalAs(UnmanagedType.LPWStr)] string message);

#if DEBUG
private string LogLines = null;
#endif
private object GlobalLock = "";
private bool IsLogOn;
private bool IsFileLogOn;
private string TraceFilePath;
}
}
4 changes: 2 additions & 2 deletions SinglePluginHost/Properties/AssemblyInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@
// You can specify all the values or you can default the Build and Revision Numbers
// by using the '*' as shown below:
// [assembly: AssemblyVersion("1.0.*")]
[assembly: AssemblyVersion("1.0.0.44")]
[assembly: AssemblyFileVersion("1.0.0.45")]
[assembly: AssemblyVersion("1.0.0.60")]
[assembly: AssemblyFileVersion("1.0.0.60")]
[assembly: NeutralResourcesLanguage("en-US")]

0 comments on commit 1196db9

Please sign in to comment.