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;
3using Waher.Events;
7
9{
13 public abstract class LogFunction : FunctionMultiVariate
14 {
23 : base(new ScriptNode[] { Message }, new ArgumentType[] { ArgumentType.Scalar }, Start, Length, Expression)
24 {
25 }
26
36 : base(new ScriptNode[] { Message, Tags }, new ArgumentType[] { ArgumentType.Scalar, ArgumentType.Normal },
38 {
39 }
40
44 public override string[] DefaultArgumentNames => new string[] { "Message" };
45
53 {
54 object M = Arguments[0].AssociatedObjectValue;
55 string Object = string.Empty;
56 string Actor = string.Empty;
57 string EventId = string.Empty;
58 string Facility = string.Empty;
59 string Module = string.Empty;
60 string StackTrace = string.Empty;
61 EventLevel Level = EventLevel.Minor;
63 bool Detailed = false;
64
65 if (!(M is string Message))
66 {
67 if (M is Exception ex)
68 {
69 string s;
70
71 Message = ex.Message;
72 Object = ex is IEventObject Obj2 && !string.IsNullOrEmpty(s = Obj2.Object) ? s : Object;
73 Actor = ex is IEventActor Act && !string.IsNullOrEmpty(s = Act.Actor) ? s : Actor;
74 EventId = ex is IEventId EvId && !string.IsNullOrEmpty(s = EvId.EventId) ? s : EventId;
75 Level = ex is IEventLevel Lvl && Lvl.Level.HasValue ? Lvl.Level.Value : Level;
76 Facility = ex is IEventFacility EvFa && !string.IsNullOrEmpty(s = EvFa.Facility) ? s : Facility;
77 Module = ex is IEventModule Mod && !string.IsNullOrEmpty(s = Mod.Module) ? s : ex.Source;
78 StackTrace = Log.CleanStackTrace(ex.StackTrace);
79
80 if (ex is IEventTags Tags2)
81 {
82 KeyValuePair<string, object>[] Tags3 = Tags2.Tags;
83 if (!(Tags3 is null))
84 {
86 Tags.AddRange(Tags3);
87 }
88 }
89
90 Detailed = true;
91 }
92 else
93 Message = M?.ToString() ?? string.Empty;
94 }
95
96 if (Arguments.Length > 1 &&
97 Arguments[1].AssociatedObjectValue is IDictionary<string, IElement> Obj)
98 {
99 foreach (KeyValuePair<string, IElement> P in Obj)
100 {
101 switch (P.Key.ToUpper())
102 {
103 case "OBJECT":
104 Object = ToString(P.Value) ?? string.Empty;
105 break;
106
107 case "ACTOR":
108 Actor = ToString(P.Value) ?? string.Empty;
109 break;
110
111 case "EVENTID":
112 EventId = ToString(P.Value) ?? string.Empty;
113 break;
114
115 case "FACILITY":
116 Facility = ToString(P.Value) ?? string.Empty;
117 break;
118
119 case "MODULE":
120 Module = ToString(P.Value) ?? string.Empty;
121 break;
122
123 case "STACKTRACE":
124 StackTrace = ToString(P.Value) ?? string.Empty;
125 break;
126
127 case "LEVEL":
128 if (P.Value.AssociatedObjectValue is EventLevel L ||
129 Enum.TryParse(ToString(P.Value) ?? string.Empty, out L))
130 {
131 Level = L;
132 }
133 else
134 {
135 if (Tags is null)
137
138 Tags.Add(new KeyValuePair<string, object>(P.Key, P.Value.AssociatedObjectValue));
139 }
140 break;
141
142 default:
143 if (Tags is null)
145
146 Tags.Add(new KeyValuePair<string, object>(P.Key, P.Value.AssociatedObjectValue));
147 break;
148 }
149 }
150
151 Detailed = true;
152 }
153
154 if (Detailed)
155 {
156 this.DoLog(Message, Object, Actor, EventId, Level, Facility, Module, StackTrace,
157 Tags?.ToArray() ?? Array.Empty<KeyValuePair<string, object>>());
158 }
159 else
160 this.DoLog(Message);
161
162 return Arguments[0];
163 }
164
169 public abstract void DoLog(string Message);
170
183 public abstract void DoLog(string Message, string Object, string Actor, string EventId, EventLevel Level,
184 string Facility, string Module, string StackTrace, params KeyValuePair<string, object>[] Tags);
185 }
186}
Static class managing the application event log. Applications and services log events on this static ...
Definition: Log.cs:14
static string CleanStackTrace(string StackTrace)
Cleans a Stack Trace string, removing entries from the asynchronous execution model,...
Definition: Log.cs:194
A chunked list is a linked list of chunks of objects of type T .
Definition: ChunkedList.cs:54
void AddRange(IEnumerable< T > Collection)
Adds a range of elements (last) to the list.
void Add(T Item)
Adds an item to the collection.
Definition: ChunkedList.cs:272
Class managing a script expression.
Definition: Expression.cs:41
Abstract base class for log functions
Definition: LogFunction.cs:14
override IElement Evaluate(IElement[] Arguments, Variables Variables)
Evaluates the function.
Definition: LogFunction.cs:52
LogFunction(ScriptNode Message, int Start, int Length, Expression Expression)
Abstract base class for log functions
Definition: LogFunction.cs:22
LogFunction(ScriptNode Message, ScriptNode Tags, int Start, int Length, Expression Expression)
Abstract base class for log functions
Definition: LogFunction.cs:35
override string[] DefaultArgumentNames
Default Argument names
Definition: LogFunction.cs:44
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
override string ToString()
Definition: ScriptNode.cs:359
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:21
EventLevel
Event level.
Definition: EventLevel.cs:7
ArgumentType
Type of parameter used in a function definition or a lambda definition.
Definition: IFunction.cs:9