Skip to content

Commit

Permalink
Logging: use consistent logging approach
Browse files Browse the repository at this point in the history
  • Loading branch information
jbe2277 committed Nov 20, 2023
1 parent 99dc35d commit 1c49d36
Show file tree
Hide file tree
Showing 5 changed files with 13 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -208,7 +208,7 @@ private void CloseAll()
string fileExtension = Path.GetExtension(fileName);
if (!supportedFileExtensions.Contains(fileExtension))
{
Trace.TraceError(string.Format(CultureInfo.InvariantCulture, "The extension of the file '{0}' is not supported.", fileName));
Log.Default.Error("The extension of the file '{0}' is not supported.", fileName);
messageService.ShowError(shellService.ShellView, Resources.OpenFileUnsupportedExtension, fileName);
return null;
}
Expand All @@ -222,7 +222,7 @@ private void CloseAll()

private static DocumentContent LoadDocumentContent(DocumentFile documentFile)
{
Trace.WriteLine(">> Load document content: " + documentFile.FileName);
Log.Default.Trace("Load document content: {0}", documentFile.FileName);
using var stream = new FileStream(documentFile.FileName ?? throw new InvalidOperationException("FileName is null"), FileMode.Open, FileAccess.Read);
using var reader = new StreamReader(stream, Encoding.UTF8);
var documentContent = new DocumentContent() { Code = reader.ReadToEnd() };
Expand All @@ -243,9 +243,9 @@ private void SaveCore(DocumentFile document, string fileName)
document.FileName = fileName;
document.ResetModified();
}
catch (Exception e)
catch (Exception ex)
{
Trace.TraceError(e.ToString());
Log.Default.Error("Save error: {0}", ex);
messageService.ShowError(shellService.ShellView, Resources.SaveFileError, fileName);
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/DotNetPad/DotNetPad.Domain/DocumentFile.cs
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ public DocumentContent? Content
}
catch (Exception ex)
{
Trace.TraceError(ex.ToString());
Log.Default.Error("LoadContent error: {0}", ex);
LoadError = ex;
}

Expand Down
2 changes: 1 addition & 1 deletion src/DotNetPad/DotNetPad.Domain/Log.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,5 @@ namespace Waf.DotNetPad.Domain;

public static class Log
{
public static TraceSource Default { get; } = new TraceSource("App");
public static TraceSource Default { get; } = new("App");
}
2 changes: 1 addition & 1 deletion src/DotNetPad/DotNetPad.Domain/PerformanceTrace.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,5 @@ public PerformanceTrace(string name, DocumentFile document) : this(document.File
{
}

public void Dispose() => Trace.WriteLine(">>> " + name + ": " + stopwatch.ElapsedMilliseconds + " ms");
public void Dispose() => Log.Default.Trace(">>> {0}: {1} ms", name, stopwatch.ElapsedMilliseconds);
}
7 changes: 6 additions & 1 deletion src/DotNetPad/DotNetPad.Presentation/App.xaml.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System.ComponentModel.Composition;
using System.ComponentModel.Composition.Hosting;
using System.Diagnostics;
using System.Globalization;
using System.Waf;
using System.Waf.Applications;
Expand All @@ -21,10 +22,13 @@ protected override void OnStartup(StartupEventArgs e)
base.OnStartup(e);

#if !DEBUG
Log.Default.Switch.Level = SourceLevels.Information;
DispatcherUnhandledException += AppDispatcherUnhandledException;
AppDomain.CurrentDomain.UnhandledException += AppDomainUnhandledException;
#else
Log.Default.Switch.Level = SourceLevels.Verbose;
#endif

Log.Default.Info("{0} {1} is starting; OS: {2}", ApplicationInfo.ProductName, ApplicationInfo.Version, Environment.OSVersion);
catalog = new();
catalog.Catalogs.Add(new AssemblyCatalog(typeof(WafConfiguration).Assembly));
catalog.Catalogs.Add(new AssemblyCatalog(typeof(ShellViewModel).Assembly));
Expand All @@ -46,6 +50,7 @@ protected override void OnExit(ExitEventArgs e)
container.Dispose();
catalog.Dispose();
base.OnExit(e);
Log.Default.Info("{0} closed", ApplicationInfo.ProductName);
}

private static void AppDispatcherUnhandledException(object sender, DispatcherUnhandledExceptionEventArgs e) => HandleException(e.Exception, false);
Expand Down

0 comments on commit 1c49d36

Please sign in to comment.