本文整理汇总了C#中NLog.LogReceiverService.NLogEvents类的典型用法代码示例。如果您正苦于以下问题:C# NLogEvents类的具体用法?C# NLogEvents怎么用?C# NLogEvents使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
NLogEvents类属于NLog.LogReceiverService命名空间,在下文中一共展示了NLogEvents类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: ProcessLogMessages
/// <summary>
/// Processes the log messages.
/// </summary>
/// <param name="events">The events to process.</param>
public void ProcessLogMessages(NLogEvents events)
{
var baseTimeUtc = new DateTime(events.BaseTimeUtc, DateTimeKind.Utc);
var logEvents = new LogEventInfo[events.Events.Length];
// convert transport representation of log events into workable LogEventInfo[]
for (int j = 0; j < events.Events.Length; ++j)
{
var ev = events.Events[j];
LogLevel level = LogLevel.FromOrdinal(ev.LevelOrdinal);
string loggerName = events.Strings[ev.LoggerOrdinal];
var logEventInfo = new LogEventInfo();
logEventInfo.Level = level;
logEventInfo.LoggerName = loggerName;
logEventInfo.TimeStamp = baseTimeUtc.AddTicks(ev.TimeDelta);
logEventInfo.Message = events.Strings[ev.MessageOrdinal];
logEventInfo.Properties.Add("ClientName", events.ClientName);
for (int i = 0; i < events.LayoutNames.Count; ++i)
{
logEventInfo.Properties.Add(events.LayoutNames[i], ev.Values[i]);
}
logEvents[j] = logEventInfo;
}
this.ProcessLogMessages(logEvents);
}
示例2: ToLogEventInfoTest
public void ToLogEventInfoTest()
{
var events = new NLogEvents
{
BaseTimeUtc = new DateTime(2010, 1, 1, 0, 0, 0, 0, DateTimeKind.Utc).Ticks,
ClientName = "foo",
LayoutNames = new StringCollection { "foo", "bar", "baz" },
Strings = new StringCollection { "logger1", "logger2", "logger3", "zzz", "message1" },
Events =
new[]
{
new NLogEvent
{
Id = 1,
LevelOrdinal = 2,
LoggerOrdinal = 0,
TimeDelta = 30000000,
MessageOrdinal = 4,
Values = "0|1|2"
},
new NLogEvent
{
Id = 2,
LevelOrdinal = 3,
LoggerOrdinal = 2,
MessageOrdinal = 4,
TimeDelta = 30050000,
Values = "0|1|3",
}
}
};
var converted = events.ToEventInfo();
Assert.Equal(2, converted.Count);
Assert.Equal("message1", converted[0].FormattedMessage);
Assert.Equal("message1", converted[1].FormattedMessage);
Assert.Equal(new DateTime(2010, 1, 1, 0, 0, 3, 0, DateTimeKind.Utc), converted[0].TimeStamp.ToUniversalTime());
Assert.Equal(new DateTime(2010, 1, 1, 0, 0, 3, 5, DateTimeKind.Utc), converted[1].TimeStamp.ToUniversalTime());
Assert.Equal("logger1", converted[0].LoggerName);
Assert.Equal("logger3", converted[1].LoggerName);
Assert.Equal(LogLevel.Info, converted[0].Level);
Assert.Equal(LogLevel.Warn, converted[1].Level);
Layout fooLayout = "${event-context:foo}";
Layout barLayout = "${event-context:bar}";
Layout bazLayout = "${event-context:baz}";
Assert.Equal("logger1", fooLayout.Render(converted[0]));
Assert.Equal("logger1", fooLayout.Render(converted[1]));
Assert.Equal("logger2", barLayout.Render(converted[0]));
Assert.Equal("logger2", barLayout.Render(converted[1]));
Assert.Equal("logger3", bazLayout.Render(converted[0]));
Assert.Equal("zzz", bazLayout.Render(converted[1]));
}
示例3: ProcessLogMessages
public void ProcessLogMessages(NLogEvents nevents)
{
var events = nevents.ToEventInfo("Client.");
Console.WriteLine("in: {0} {1}", nevents.Events.Length, events.Count);
foreach (var ev in events)
{
var logger = LogManager.GetLogger(ev.LoggerName);
logger.Log(ev);
}
}
示例4: CompareSerializationFormats
public void CompareSerializationFormats()
{
var events = new NLogEvents
{
BaseTimeUtc = DateTime.UtcNow.Ticks,
ClientName = "foo",
LayoutNames = new StringCollection { "foo", "bar", "baz" },
Strings = new StringCollection { "logger1", "logger2", "logger3" },
Events =
new[]
{
new NLogEvent
{
Id = 1,
LevelOrdinal = 2,
LoggerOrdinal = 0,
TimeDelta = 34,
Values = "1|2|3"
},
new NLogEvent
{
Id = 2,
LevelOrdinal = 3,
LoggerOrdinal = 2,
TimeDelta = 345,
Values = "1|2|3",
}
}
};
var serializer1 = new XmlSerializer(typeof(NLogEvents));
var sw1 = new StringWriter();
using (var writer1 = XmlWriter.Create(sw1, new XmlWriterSettings { Indent = true }))
{
var namespaces = new XmlSerializerNamespaces();
namespaces.Add("i", "http://www.w3.org/2001/XMLSchema-instance");
serializer1.Serialize(writer1, events, namespaces);
}
var serializer2 = new DataContractSerializer(typeof(NLogEvents));
var sw2 = new StringWriter();
using (var writer2 = XmlWriter.Create(sw2, new XmlWriterSettings { Indent = true }))
{
serializer2.WriteObject(writer2, events);
}
var xml1 = sw1.ToString();
var xml2 = sw2.ToString();
Assert.AreEqual(xml1, xml2);
}
示例5: LogEvents
public void LogEvents(NLogEvents events)
{
Source source = ParseAndCreateSource(events.ClientName);
if (source == null)
{
Log.Error("Ignoring received events");
return;
}
var batch = _batchRepository.Create(source);
var timberMillEvents = ExtractLogEvents(events, batch);
timberMillEvents.ForEach(_eventRepository.Save);
Log.Info("Received {0} events from {1}", timberMillEvents.Count, source);
}
示例6: ProcessLogMessages
public void ProcessLogMessages(NLogEvents events)
{
DateTime baseTimeUtc = new DateTime(events.BaseTimeUtc, DateTimeKind.Utc);
foreach (var ev in events.Events)
{
LogLevel level = LogLevel.FromOrdinal(ev.LevelOrdinal);
string loggerName = events.LoggerNames[ev.LoggerOrdinal];
Logger logger = LogManager.GetLogger(loggerName);
var logEventInfo = new LogEventInfo();
logEventInfo.Level = level;
logEventInfo.LoggerName = loggerName;
logEventInfo.TimeStamp = baseTimeUtc.AddTicks(ev.TimeDelta);
logEventInfo.Properties.Add("ClientName", events.ClientName);
for (int i = 0; i < events.LayoutNames.Count; ++i)
{
logEventInfo.Properties.Add(events.LayoutNames[i], ev.Values[i]);
}
logger.Log(logEventInfo);
}
}
示例7: ExtractLogEvents
private static List<LogEvent> ExtractLogEvents(NLogEvents events, Batch batch)
{
var logEventInfoList = events.ToEventInfo().ToList();
var timberMillEvents = logEventInfoList.ConvertAll(ei => TimberMillEventFactory(batch, ei));
return timberMillEvents;
}
示例8: Send
private void Send(NLogEvents events, IEnumerable<AsyncLogEventInfo> asyncContinuations)
{
if (!this.OnSend(events, asyncContinuations))
{
return;
}
#if WCF_SUPPORTED
WcfLogReceiverClient client;
if (string.IsNullOrEmpty(this.EndpointConfigurationName))
{
// endpoint not specified - use BasicHttpBinding
Binding binding;
#if !SILVERLIGHT2
if (this.UseBinaryEncoding)
{
binding = new CustomBinding(new BinaryMessageEncodingBindingElement(), new HttpTransportBindingElement());
}
else
#endif
{
binding = new BasicHttpBinding();
}
client = new WcfLogReceiverClient(binding, new EndpointAddress(this.EndpointAddress));
}
else
{
client = new WcfLogReceiverClient(this.EndpointConfigurationName, new EndpointAddress(this.EndpointAddress));
}
client.ProcessLogMessagesCompleted += (sender, e) =>
{
// report error to the callers
foreach (var ev in asyncContinuations)
{
ev.Continuation(e.Error);
}
// send any buffered events
this.SendBufferedEvents();
};
this.inCall = true;
#if SILVERLIGHT
if (!Deployment.Current.Dispatcher.CheckAccess())
{
Deployment.Current.Dispatcher.BeginInvoke(() => client.ProcessLogMessagesAsync(events));
}
else
{
client.ProcessLogMessagesAsync(events);
}
#else
client.ProcessLogMessagesAsync(events);
#endif
#else
var client = new SoapLogReceiverClient(this.EndpointAddress);
this.inCall = true;
client.BeginProcessLogMessages(
events,
result =>
{
Exception exception = null;
try
{
client.EndProcessLogMessages(result);
}
catch (Exception ex)
{
if (ex.MustBeRethrown())
{
throw;
}
exception = ex;
}
// report error to the callers
foreach (var ev in asyncContinuations)
{
ev.Continuation(exception);
}
// send any buffered events
this.SendBufferedEvents();
},
null);
#endif
}
示例9: BeginProcessLogMessages
/// <summary>
/// Begins processing of log messages.
/// </summary>
/// <param name="events">The events.</param>
/// <param name="callback">The callback.</param>
/// <param name="asyncState">Asynchronous state.</param>
/// <returns>
/// IAsyncResult value which can be passed to <see cref="ILogReceiverClient.EndProcessLogMessages"/>.
/// </returns>
public IAsyncResult BeginProcessLogMessages(NLogEvents events, AsyncCallback callback, object asyncState)
{
return this.BeginInvoke("ProcessLogMessages", new object[] { events }, callback, asyncState);
}
示例10: ProcessLogMessagesAsync
/// <summary>
/// Processes the log messages asynchronously.
/// </summary>
/// <param name="events">The events to send.</param>
public void ProcessLogMessagesAsync(NLogEvents events)
{
if (m_useOneWay)
{
m_oneWayClient.ProcessLogMessagesAsync(events);
}
else
{
m_twoWayClient.ProcessLogMessagesAsync(events);
}
}
示例11: TranslateEvent
private NLogEvent TranslateEvent(LogEventInfo eventInfo, NLogEvents context, Dictionary<string, int> stringTable)
{
var nlogEvent = new NLogEvent();
nlogEvent.Id = eventInfo.SequenceID;
nlogEvent.MessageOrdinal = AddValueAndGetStringOrdinal(context, stringTable, eventInfo.FormattedMessage);
nlogEvent.LevelOrdinal = eventInfo.Level.Ordinal;
nlogEvent.LoggerOrdinal = AddValueAndGetStringOrdinal(context, stringTable, eventInfo.LoggerName);
nlogEvent.TimeDelta = eventInfo.TimeStamp.ToUniversalTime().Ticks - context.BaseTimeUtc;
for (int i = 0; i < this.Parameters.Count; ++i)
{
var param = this.Parameters[i];
var value = param.Layout.Render(eventInfo);
int stringIndex = AddValueAndGetStringOrdinal(context, stringTable, value);
nlogEvent.ValueIndexes.Add(stringIndex);
}
// layout names beyond Parameters.Count are per-event property names.
for (int i = this.Parameters.Count; i < context.LayoutNames.Count; ++i)
{
string value;
object propertyValue;
if (eventInfo.Properties.TryGetValue(context.LayoutNames[i], out propertyValue))
{
value = Convert.ToString(propertyValue, CultureInfo.InvariantCulture);
}
else
{
value = string.Empty;
}
int stringIndex = AddValueAndGetStringOrdinal(context, stringTable, value);
nlogEvent.ValueIndexes.Add(stringIndex);
}
if (eventInfo.Exception != null)
{
nlogEvent.ValueIndexes.Add(AddValueAndGetStringOrdinal(context, stringTable, eventInfo.Exception.ToString()));
}
return nlogEvent;
}
示例12: TranslateLogEvents
private NLogEvents TranslateLogEvents(AsyncLogEventInfo[] logEvents)
{
if (logEvents.Length == 0 && !LogManager.ThrowExceptions)
{
InternalLogger.Error("LogEvents array is empty, sending empty event...");
return new NLogEvents();
}
string clientID = string.Empty;
if (this.ClientId != null)
{
clientID = this.ClientId.Render(logEvents[0].LogEvent);
}
var networkLogEvents = new NLogEvents
{
ClientName = clientID,
LayoutNames = new StringCollection(),
Strings = new StringCollection(),
BaseTimeUtc = logEvents[0].LogEvent.TimeStamp.ToUniversalTime().Ticks
};
var stringTable = new Dictionary<string, int>();
for (int i = 0; i < this.Parameters.Count; ++i)
{
networkLogEvents.LayoutNames.Add(this.Parameters[i].Name);
}
if (this.IncludeEventProperties)
{
for (int i = 0; i < logEvents.Length; ++i)
{
var ev = logEvents[i].LogEvent;
// add all event-level property names in 'LayoutNames' collection.
foreach (var prop in ev.Properties)
{
string propName = prop.Key as string;
if (propName != null)
{
if (!networkLogEvents.LayoutNames.Contains(propName))
{
networkLogEvents.LayoutNames.Add(propName);
}
}
}
}
}
networkLogEvents.Events = new NLogEvent[logEvents.Length];
for (int i = 0; i < logEvents.Length; ++i)
{
networkLogEvents.Events[i] = this.TranslateEvent(logEvents[i].LogEvent, networkLogEvents, stringTable);
}
return networkLogEvents;
}
示例13: OnSend
/// <summary>
/// Called when log events are being sent (test hook).
/// </summary>
/// <param name="events">The events.</param>
/// <param name="asyncContinuations">The async continuations.</param>
/// <returns>True if events should be sent, false to stop processing them.</returns>
protected internal virtual bool OnSend(NLogEvents events, IEnumerable<AsyncLogEventInfo> asyncContinuations)
{
return true;
}
示例14: NoLayoutsTest
public void NoLayoutsTest()
{
var events = new NLogEvents
{
BaseTimeUtc = new DateTime(2010, 1, 1, 0, 0, 0, 0, DateTimeKind.Utc).Ticks,
ClientName = "foo",
LayoutNames = new StringCollection(),
Strings = new StringCollection { "logger1", "logger2", "logger3", "zzz", "message1" },
Events =
new[]
{
new NLogEvent
{
Id = 1,
LevelOrdinal = 2,
LoggerOrdinal = 0,
TimeDelta = 30000000,
MessageOrdinal = 4,
Values = null,
},
new NLogEvent
{
Id = 2,
LevelOrdinal = 3,
LoggerOrdinal = 2,
MessageOrdinal = 4,
TimeDelta = 30050000,
Values = null,
}
}
};
var converted = events.ToEventInfo();
Assert.Equal(2, converted.Count);
Assert.Equal("message1", converted[0].FormattedMessage);
Assert.Equal("message1", converted[1].FormattedMessage);
Assert.Equal(new DateTime(2010, 1, 1, 0, 0, 3, 0, DateTimeKind.Utc), converted[0].TimeStamp.ToUniversalTime());
Assert.Equal(new DateTime(2010, 1, 1, 0, 0, 3, 5, DateTimeKind.Utc), converted[1].TimeStamp.ToUniversalTime());
Assert.Equal("logger1", converted[0].LoggerName);
Assert.Equal("logger3", converted[1].LoggerName);
Assert.Equal(LogLevel.Info, converted[0].Level);
Assert.Equal(LogLevel.Warn, converted[1].Level);
}
示例15: TranslateLogEvents
private NLogEvents TranslateLogEvents(AsyncLogEventInfo[] logEvents)
{
string clientID = string.Empty;
if (ClientId != null)
clientID = ClientId.Render(logEvents[0].LogEvent);
var networkLogEvents = new NLogEvents
{
ClientName = clientID,
LayoutNames = new StringCollection(),
Strings = new StringCollection(),
BaseTimeUtc = logEvents[0].LogEvent.TimeStamp.ToUniversalTime().Ticks
};
var stringTable = new Dictionary<string, int>();
for (int i = 0; i < Parameters.Count; ++i)
{
networkLogEvents.LayoutNames.Add(Parameters[i].Name);
}
if (IncludeEventProperties)
{
for (int i = 0; i < logEvents.Length; ++i)
{
var ev = logEvents[i].LogEvent;
// add all event-level property names in 'LayoutNames' collection.
foreach (var prop in ev.Properties)
{
string propName = prop.Key as string;
if (propName != null)
{
if (!networkLogEvents.LayoutNames.Contains(propName))
{
networkLogEvents.LayoutNames.Add(propName);
}
}
}
}
}
networkLogEvents.Events = new NLogEvent[logEvents.Length];
for (int i = 0; i < logEvents.Length; ++i)
{
networkLogEvents.Events[i] = TranslateEvent(logEvents[i].LogEvent, networkLogEvents, stringTable);
}
return networkLogEvents;
}