Neuron®
The Neuron® is the basis for the creation of open and secure federated networks for smart societies.
Loading...
Searching...
No Matches
LogService.cs
1using System.Diagnostics;
3using Waher.Events;
8
10{
11 [Singleton]
12 internal sealed class LogService : ILogService
13 {
14 private const string startupCrashFileName = "CrashDump.txt";
15 private string bareJid = string.Empty;
16 private bool repairRequested = false;
17
23 public void AddListener(IEventSink EventSink)
24 {
26 this.bareJid = XmppEventSink.Client?.BareJID ?? string.Empty;
27
28 foreach (IEventSink Sink in Log.Sinks)
29 {
30 if (Sink == EventSink)
31 return;
32 }
33
35 }
36
41 public void RemoveListener(IEventSink EventSink)
42 {
43 if (EventSink is not null)
45 }
46
52 public void LogWarning(string Message, params KeyValuePair<string, object?>[] Tags)
53 {
54 Log.Warning(Message, string.Empty, this.bareJid, [.. this.GetParameters(Tags)]);
55 }
56
61 public void LogException(Exception ex)
62 {
63 this.LogException(ex, []);
64 }
65
71 public void LogException(Exception ex, params KeyValuePair<string, object?>[] extraParameters)
72 {
73 ex = Log.UnnestException(ex);
74
75 Debug.WriteLine(ex.ToString());
76 Log.Exception(ex, string.Empty, this.bareJid, [.. this.GetParameters(extraParameters)]);
77
78 if (ex is InconsistencyException && !this.repairRequested)
79 {
80 this.repairRequested = true;
81 Task.Run(RestartForRepair);
82 }
83 }
84
90 public void LogAlert(string Message, params KeyValuePair<string, object?>[] Tags)
91 {
92 Log.Alert(Message, string.Empty, this.bareJid, [.. this.GetParameters(Tags)]);
93 }
94
95 private static async Task RestartForRepair()
96 {
97 ServiceRef.StorageService.FlagForRepair();
98
99 await ServiceRef.UiService.DisplayAlert(ServiceRef.Localizer[nameof(AppResources.ErrorTitle)],
100 ServiceRef.Localizer[nameof(AppResources.RepairRestart)],
101 ServiceRef.Localizer[nameof(AppResources.Ok)]);
102
103 try
104 {
105 await ServiceRef.PlatformSpecific.CloseApplication();
106 }
107 catch (Exception)
108 {
109 Environment.Exit(0);
110 }
111 }
112
118 public void SaveExceptionDump(string Title, string StackTrace)
119 {
120 StackTrace = Log.CleanStackTrace(StackTrace);
121
122 string contents;
123 string fileName = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData), startupCrashFileName);
124
125 if (File.Exists(fileName))
126 contents = File.ReadAllText(fileName);
127 else
128 contents = string.Empty;
129
130 File.WriteAllText(fileName, Title + Environment.NewLine + StackTrace + Environment.NewLine + contents);
131 }
132
137 public string LoadExceptionDump()
138 {
139 string contents;
140 string fileName = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData), startupCrashFileName);
141
142 if (File.Exists(fileName))
143 contents = File.ReadAllText(fileName);
144 else
145 contents = string.Empty;
146
147 return contents;
148 }
149
153 public void DeleteExceptionDump()
154 {
155 string fileName = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData), startupCrashFileName);
156
157 if (File.Exists(fileName))
158 File.Delete(fileName);
159 }
160
166 public IList<KeyValuePair<string, object?>> GetParameters(params KeyValuePair<string, object?>[] Tags)
167 {
168 List<KeyValuePair<string, object?>> Result =
169 [
170 new KeyValuePair<string, object?>("Platform", DeviceInfo.Platform),
171 new KeyValuePair<string, object?>("RuntimeVersion", typeof(LogService).Assembly.ImageRuntimeVersion),
172 new KeyValuePair<string, object?>("AppVersion", AppInfo.VersionString),
173 new KeyValuePair<string, object?>("Manufacturer", DeviceInfo.Manufacturer),
174 new KeyValuePair<string, object?>("Device Model", DeviceInfo.Model),
175 new KeyValuePair<string, object?>("Device Name", DeviceInfo.Name),
176 new KeyValuePair<string, object?>("OS", DeviceInfo.VersionString),
177 new KeyValuePair<string, object?>("Platform", DeviceInfo.Platform.ToString()),
178 new KeyValuePair<string, object?>("Device Type", DeviceInfo.DeviceType.ToString()),
179 ];
180
181 if (Tags is not null)
182 Result.AddRange(Tags);
183
184 return Result;
185 }
186 }
187}
Base class for event sinks.
Definition: EventSink.cs:9
Filters incoming events and passes remaining events to a secondary event sink.
Definition: EventFilter.cs:9
IEventSink SecondarySink
Secondary event sink receiving the events passing the filter.
Definition: EventFilter.cs:164
Static class managing the application event log. Applications and services log events on this static ...
Definition: Log.cs:13
static string CleanStackTrace(string StackTrace)
Cleans a Stack Trace string, removing entries from the asynchronous execution model,...
Definition: Log.cs:184
static IEventSink[] Sinks
Registered sinks.
Definition: Log.cs:122
static void Register(IEventSink EventSink)
Registers an event sink with the event log. Call Unregister(IEventSink) to unregister it,...
Definition: Log.cs:29
static void Exception(Exception Exception, string Object, string Actor, string EventId, EventLevel Level, string Facility, string Module, params KeyValuePair< string, object >[] Tags)
Logs an exception. Event type will be determined by the severity of the exception.
Definition: Log.cs:1647
static void Warning(string Message, string Object, string Actor, string EventId, EventLevel Level, string Facility, string Module, string StackTrace, params KeyValuePair< string, object >[] Tags)
Logs a warning event.
Definition: Log.cs:566
static Exception UnnestException(Exception Exception)
Unnests an exception, to extract the relevant inner exception.
Definition: Log.cs:818
static bool Unregister(IEventSink EventSink)
Unregisters an event sink from the event log.
Definition: Log.cs:46
static void Alert(string Message, string Object, string Actor, string EventId, EventLevel Level, string Facility, string Module, string StackTrace, params KeyValuePair< string, object >[] Tags)
Logs an alert event.
Definition: Log.cs:1227
Event sink sending events to a destination over the XMPP network.
XmppClient Client
XMPP Client
Database inconsistency exception. Raised when an inconsistency in the database has been found.
Interface for all event sinks.
Definition: IEventSink.cs:9