Neuron®
The Neuron® is the basis for the creation of open and secure federated networks for smart societies.
Loading...
Searching...
No Matches
LogFunction.cs
1using System;
2using System.Collections.Generic;
3using Waher.Events;
6
8{
12 public abstract class LogFunction : FunctionMultiVariate
13 {
22 : base(new ScriptNode[] { Message }, new ArgumentType[] { ArgumentType.Scalar }, Start, Length, Expression)
23 {
24 }
25
35 : base(new ScriptNode[] { Message, Tags }, new ArgumentType[] { ArgumentType.Scalar, ArgumentType.Normal },
37 {
38 }
39
43 public override string[] DefaultArgumentNames => new string[] { "Message" };
44
52 {
53 object M = Arguments[0].AssociatedObjectValue;
54 string Object = string.Empty;
55 string Actor = string.Empty;
56 string EventId = string.Empty;
57 string Facility = string.Empty;
58 string Module = string.Empty;
59 string StackTrace = string.Empty;
60 EventLevel Level = EventLevel.Minor;
61 List<KeyValuePair<string, object>> Tags = null;
62 bool Detailed = false;
63
64 if (!(M is string Message))
65 {
66 if (M is Exception ex)
67 {
68 string s;
69
70 Message = ex.Message;
71 Object = ex is IEventObject Obj2 && !string.IsNullOrEmpty(s = Obj2.Object) ? s : Object;
72 Actor = ex is IEventActor Act && !string.IsNullOrEmpty(s = Act.Actor) ? s : Actor;
73 EventId = ex is IEventId EvId && !string.IsNullOrEmpty(s = EvId.EventId) ? s : EventId;
74 Level = ex is IEventLevel Lvl && Lvl.Level.HasValue ? Lvl.Level.Value : Level;
75 Facility = ex is IEventFacility EvFa && !string.IsNullOrEmpty(s = EvFa.Facility) ? s : Facility;
76 Module = ex is IEventModule Mod && !string.IsNullOrEmpty(s = Mod.Module) ? s : ex.Source;
77 StackTrace = Log.CleanStackTrace(ex.StackTrace);
78
79 if (ex is IEventTags Tags2)
80 {
81 KeyValuePair<string, object>[] Tags3 = Tags2.Tags;
82 if (!(Tags3 is null))
83 {
84 Tags = new List<KeyValuePair<string, object>>();
85 Tags.AddRange(Tags3);
86 }
87 }
88
89 Detailed = true;
90 }
91 else
92 Message = M?.ToString() ?? string.Empty;
93 }
94
95 if (Arguments.Length > 1 &&
96 Arguments[1].AssociatedObjectValue is IDictionary<string, IElement> Obj)
97 {
98 foreach (KeyValuePair<string, IElement> P in Obj)
99 {
100 switch (P.Key.ToUpper())
101 {
102 case "OBJECT":
103 Object = P.Value.AssociatedObjectValue?.ToString() ?? string.Empty;
104 break;
105
106 case "ACTOR":
107 Actor = P.Value.AssociatedObjectValue?.ToString() ?? string.Empty;
108 break;
109
110 case "EVENTID":
111 EventId = P.Value.AssociatedObjectValue?.ToString() ?? string.Empty;
112 break;
113
114 case "FACILITY":
115 Facility = P.Value.AssociatedObjectValue?.ToString() ?? string.Empty;
116 break;
117
118 case "MODULE":
119 Module = P.Value.AssociatedObjectValue?.ToString() ?? string.Empty;
120 break;
121
122 case "STACKTRACE":
123 StackTrace = P.Value.AssociatedObjectValue?.ToString() ?? string.Empty;
124 break;
125
126 case "LEVEL":
127 if (P.Value.AssociatedObjectValue is EventLevel L ||
128 Enum.TryParse(P.Value.AssociatedObjectValue?.ToString() ?? string.Empty, out L))
129 {
130 Level = L;
131 }
132 else
133 {
134 if (Tags is null)
135 Tags = new List<KeyValuePair<string, object>>();
136
137 Tags.Add(new KeyValuePair<string, object>(P.Key, P.Value.AssociatedObjectValue));
138 }
139 break;
140
141 default:
142 if (Tags is null)
143 Tags = new List<KeyValuePair<string, object>>();
144
145 Tags.Add(new KeyValuePair<string, object>(P.Key, P.Value.AssociatedObjectValue));
146 break;
147 }
148 }
149
150 Detailed = true;
151 }
152
153 if (Detailed)
154 {
155 this.DoLog(Message, Object, Actor, EventId, Level, Facility, Module, StackTrace,
156 Tags?.ToArray() ?? new KeyValuePair<string, object>[0]);
157 }
158 else
159 this.DoLog(Message);
160
161 return Arguments[0];
162 }
163
168 public abstract void DoLog(string Message);
169
182 public abstract void DoLog(string Message, string Object, string Actor, string EventId, EventLevel Level,
183 string Facility, string Module, string StackTrace, params KeyValuePair<string, object>[] Tags);
184 }
185}
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
Class managing a script expression.
Definition: Expression.cs:39
Abstract base class for log functions
Definition: LogFunction.cs:13
override IElement Evaluate(IElement[] Arguments, Variables Variables)
Evaluates the function.
Definition: LogFunction.cs:51
LogFunction(ScriptNode Message, int Start, int Length, Expression Expression)
Abstract base class for log functions
Definition: LogFunction.cs:21
LogFunction(ScriptNode Message, ScriptNode Tags, int Start, int Length, Expression Expression)
Abstract base class for log functions
Definition: LogFunction.cs:34
override string[] DefaultArgumentNames
Default Argument names
Definition: LogFunction.cs:43
abstract void DoLog(string Message)
Logs information to the event log.
abstract void DoLog(string Message, string Object, string Actor, string EventId, EventLevel Level, string Facility, string Module, string StackTrace, params KeyValuePair< string, object >[] Tags)
Logs information to the event log.
Base class for multivariate funcions.
ScriptNode[] Arguments
Function arguments.
Base class for all nodes in a parsed script tree.
Definition: ScriptNode.cs:69
int Length
Length of expression covered by node.
Definition: ScriptNode.cs:101
Expression Expression
Expression of which the node is a part.
Definition: ScriptNode.cs:177
int Start
Start position in script expression.
Definition: ScriptNode.cs:92
Collection of variables.
Definition: Variables.cs:25
Implement this interface on exception classes to allow the log to extract actor information in corres...
Definition: IEventActor.cs:8
Implement this interface on exception classes to allow the log to extract facility information in cor...
Implement this interface on exception classes to allow the log to extract Event ID information in cor...
Definition: IEventId.cs:8
Implement this interface on exception classes to allow the log to extract Event Level information in ...
Definition: IEventLevel.cs:8
Implement this interface on exception classes to allow the log to extract module information in corre...
Definition: IEventModule.cs:8
Implement this interface on exception classes to allow the log to extract object information in corre...
Definition: IEventObject.cs:8
Implement this interface on exception classes to allow the log to extract tags in corresponding event...
Definition: IEventTags.cs:10
Basic interface for all types of elements.
Definition: IElement.cs:20
EventLevel
Event level.
Definition: EventLevel.cs:7
ArgumentType
Type of parameter used in a function definition or a lambda definition.
Definition: IFunction.cs:9