本文整理汇总了C#中NLog.Common.AsyncLogEventInfo.Continuation方法的典型用法代码示例。如果您正苦于以下问题:C# AsyncLogEventInfo.Continuation方法的具体用法?C# AsyncLogEventInfo.Continuation怎么用?C# AsyncLogEventInfo.Continuation使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类NLog.Common.AsyncLogEventInfo
的用法示例。
在下文中一共展示了AsyncLogEventInfo.Continuation方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: Write
protected override void Write(AsyncLogEventInfo info)
{
try
{
String layout = this.Layout.Render(info.LogEvent);
//layout = layout.Replace("\\", "/"); //this might be useful
var json = JObject.Parse(layout).ToString(); // make sure the json is valid
var client = new WebClient();
client.Headers.Add(HttpRequestHeader.ContentType, "application/json");
UploadStringCompletedEventHandler cb = null;
cb = (s, e) =>
{
if (cb != null)
client.UploadStringCompleted -= cb;
if (e.Error != null)
{
if (e.Error is WebException)
{
var we = e.Error as WebException;
try
{
var result = JObject.Load(new JsonTextReader(new StreamReader(we.Response.GetResponseStream())));
var error = result.GetValue("error");
if (error != null)
{
info.Continuation(new Exception(result.ToString(), e.Error));
return;
}
}
catch (Exception) { info.Continuation(new Exception("Failed to send log event to HttpJsonTarget", e.Error)); }
}
info.Continuation(e.Error);
return;
}
info.Continuation(null);
};
client.UploadStringCompleted += cb;
client.UploadStringAsync(Url, "POST", json);
}
catch (Exception ex)
{
info.Continuation(ex);
}
}
示例2: Write
protected override void Write(AsyncLogEventInfo info)
{
try
{
this.SendToSlack(info);
}
catch (Exception e)
{
info.Continuation(e);
}
}
示例3: Send
private void Send(AsyncLogEventInfo info)
{
var message = Layout.Render(info.LogEvent);
var uriBuilder = new UriBuilder(BaseUrl + BotToken);
uriBuilder.Path += "/sendMessage";
var url = uriBuilder.Uri.ToString();
var builder = TelegramMessageBuilder
.Build(url, message)
.ToChat(ChatId)
.OnError(e => info.Continuation(e));
builder.Send();
}
示例4: Write
/// <summary>
/// Forwards the log event to one of the sub-targets.
/// The sub-target is randomly chosen.
/// </summary>
/// <param name="logEvent">The log event.</param>
protected override void Write(AsyncLogEventInfo logEvent)
{
if (this.Targets.Count == 0)
{
logEvent.Continuation(null);
return;
}
int selectedTarget;
lock (this.random)
{
selectedTarget = this.random.Next(this.Targets.Count);
}
this.Targets[selectedTarget].WriteAsyncLogEvent(logEvent);
}
示例5: SendToSlack
//// ----------------------------------------------------------------------------------------------------------
private void SendToSlack(AsyncLogEventInfo info)
{
var message = Layout.Render(info.LogEvent);
var slack = SlackMessageBuilder
.Build(this.WebHookUrl)
.OnError(e => info.Continuation(e))
.WithMessage(message);
if (!String.IsNullOrWhiteSpace(this.Channel.Render(info.LogEvent)))
slack.ToChannel(this.Channel.Render(info.LogEvent));
if (!String.IsNullOrWhiteSpace(this.Icon))
slack.WithIcon(this.Icon);
if (!String.IsNullOrWhiteSpace(this.Username.Render(info.LogEvent)))
slack.AsUser(this.Username.Render(info.LogEvent));
if (!this.Compact)
{
var attachment = new Attachment(message);
attachment.Color = this.GetSlackColorFromLogLevel(info.LogEvent.Level);
var exception = info.LogEvent.Exception;
if (exception != null)
{
attachment.Fields.Add(new Field("Type") { Value = exception.GetType().FullName, Short = true });
attachment.Fields.Add(new Field("Message") { Value = exception.Message, Short = true });
if (!String.IsNullOrWhiteSpace(exception.StackTrace))
attachment.Fields.Add(new Field("Stack Trace") { Value = "```" + exception.StackTrace + "```" });
}
attachment.Fields.Add(new Field("Process Name") { Value = String.Format("{0}\\{1}", (_currentProcess.MachineName != "." ? _currentProcess.MachineName : System.Environment.MachineName), _currentProcess.ProcessName), Short = true });
attachment.Fields.Add(new Field("Process PID") { Value = _currentProcess.Id.ToString(), Short = true });
slack.AddAttachment(attachment);
}
slack.Send();
}
示例6: Write
/// <summary>
/// Writes log event to the log target. Must be overridden in inheriting
/// classes.
/// </summary>
/// <param name="logEvent">Log event to be written out.</param>
protected virtual void Write(AsyncLogEventInfo logEvent)
{
try
{
this.Write(logEvent.LogEvent);
logEvent.Continuation(null);
}
catch (Exception exception)
{
if (exception.MustBeRethrown())
{
throw;
}
logEvent.Continuation(exception);
}
}
示例7: WriteAsyncLogEvent
/// <summary>
/// Writes the log to the target.
/// </summary>
/// <param name="logEvent">Log event to write.</param>
public void WriteAsyncLogEvent(AsyncLogEventInfo logEvent)
{
lock (this.SyncRoot)
{
if (!this.IsInitialized)
{
logEvent.Continuation(null);
return;
}
if (this.initializeException != null)
{
logEvent.Continuation(this.CreateInitException());
return;
}
var wrappedContinuation = AsyncHelpers.PreventMultipleCalls(logEvent.Continuation);
try
{
this.Write(logEvent.LogEvent.WithContinuation(wrappedContinuation));
}
catch (Exception exception)
{
if (exception.MustBeRethrown())
{
throw;
}
wrappedContinuation(exception);
}
}
}
示例8: Write
/// <summary>
/// Forwards the write to one of the targets from
/// the <see cref="Targets"/> collection.
/// </summary>
/// <param name="logEvent">The log event.</param>
/// <remarks>
/// The writes are routed in a round-robin fashion.
/// The first log event goes to the first target, the second
/// one goes to the second target and so on looping to the
/// first target when there are no more targets available.
/// In general request N goes to Targets[N % Targets.Count].
/// </remarks>
protected override void Write(AsyncLogEventInfo logEvent)
{
if (this.Targets.Count == 0)
{
logEvent.Continuation(null);
return;
}
int selectedTarget;
lock (this.lockObject)
{
selectedTarget = this.currentTarget;
this.currentTarget = (this.currentTarget + 1) % this.Targets.Count;
}
this.Targets[selectedTarget].WriteAsyncLogEvent(logEvent);
}
示例9: Write
/// <summary>
/// Writes log event to the wrapped target if the current <see cref="MessagesWrittenCount"/> is lower than <see cref="MessageLimit"/>.
/// If the <see cref="MessageLimit"/> is already reached, no log event will be written to the wrapped target.
/// <see cref="MessagesWrittenCount"/> resets when the current <see cref="Interval"/> is expired.
/// </summary>
/// <param name="logEvent">Log event to be written out.</param>
protected override void Write(AsyncLogEventInfo logEvent)
{
if (IsIntervalExpired())
{
ResetInterval();
InternalLogger.Debug("LimitingWrapper '{0}': new interval of '{1}' started.", Name, Interval);
}
if (MessagesWrittenCount < MessageLimit)
{
this.WrappedTarget.WriteAsyncLogEvent(logEvent);
MessagesWrittenCount++;
}
else
{
logEvent.Continuation(null);
InternalLogger.Trace("LimitingWrapper '{0}': discarded event, because MessageLimit of '{1}' was reached.", Name, MessageLimit);
}
}
示例10: Write
/// <summary>
/// Writes the specified log event to the wrapped target, retrying and pausing in case of an error.
/// </summary>
/// <param name="logEvent">The log event.</param>
protected override void Write(AsyncLogEventInfo logEvent)
{
AsyncContinuation continuation = null;
int counter = 0;
continuation = ex =>
{
if (ex == null)
{
logEvent.Continuation(null);
return;
}
int retryNumber = Interlocked.Increment(ref counter);
InternalLogger.Warn("Error while writing to '{0}': {1}. Try {2}/{3}", this.WrappedTarget, ex, retryNumber, this.RetryCount);
// exceeded retry count
if (retryNumber >= this.RetryCount)
{
InternalLogger.Warn("Too many retries. Aborting.");
logEvent.Continuation(ex);
return;
}
// sleep and try again
Thread.Sleep(this.RetryDelayMilliseconds);
this.WrappedTarget.WriteAsyncLogEvent(logEvent.LogEvent.WithContinuation(continuation));
};
this.WrappedTarget.WriteAsyncLogEvent(logEvent.LogEvent.WithContinuation(continuation));
}
示例11: Write
/// <summary>
/// Checks the condition against the passed log event.
/// If the condition is met, the log event is forwarded to
/// the wrapped target.
/// </summary>
/// <param name="logEvent">Log event.</param>
protected override void Write(AsyncLogEventInfo logEvent)
{
object v = this.Condition.Evaluate(logEvent.LogEvent);
if (boxedBooleanTrue.Equals(v))
{
this.WrappedTarget.WriteAsyncLogEvent(logEvent);
}
else
{
logEvent.Continuation(null);
}
}
示例12: WriteAsyncThreadSafe
/// <summary>
/// Writes a log event to the log target, in a thread safe manner.
/// </summary>
/// <param name="logEvent">Log event to be written out.</param>
protected virtual void WriteAsyncThreadSafe(AsyncLogEventInfo logEvent)
{
lock (this.SyncRoot)
{
if (!this.IsInitialized)
{
// In case target was Closed
logEvent.Continuation(null);
return;
}
try
{
this.Write(logEvent);
}
catch (Exception exception)
{
if (exception.MustBeRethrown())
{
throw;
}
logEvent.Continuation(exception);
}
}
}
示例13: WriteAsyncLogEvent
/// <summary>
/// Writes the log to the target.
/// </summary>
/// <param name="logEvent">Log event to write.</param>
public void WriteAsyncLogEvent(AsyncLogEventInfo logEvent)
{
if (!this.IsInitialized)
{
lock (this.SyncRoot)
{
logEvent.Continuation(null);
}
return;
}
if (this.initializeException != null)
{
lock (this.SyncRoot)
{
logEvent.Continuation(this.CreateInitException());
}
return;
}
var wrappedContinuation = AsyncHelpers.PreventMultipleCalls(logEvent.Continuation);
var wrappedLogEvent = logEvent.LogEvent.WithContinuation(wrappedContinuation);
this.WriteAsyncThreadSafe(wrappedLogEvent);
}
示例14: Write
protected override void Write(AsyncLogEventInfo logEvent)
{
Assert.True(this.FlushCount <= this.WriteCount);
Interlocked.Increment(ref this.PendingWriteCount);
ThreadPool.QueueUserWorkItem(
s =>
{
try
{
Interlocked.Increment(ref this.WriteCount);
if (this.ThrowExceptions)
{
logEvent.Continuation(new InvalidOperationException("Some problem!"));
logEvent.Continuation(new InvalidOperationException("Some problem!"));
}
else
{
logEvent.Continuation(null);
logEvent.Continuation(null);
}
}
finally
{
Interlocked.Decrement(ref this.PendingWriteCount);
}
});
}
示例15: Write
/// <summary>
/// Sends the
/// rendered logging event over the network optionally concatenating it with a newline character.
/// </summary>
/// <param name="logEvent">The logging event.</param>
protected override void Write(AsyncLogEventInfo logEvent)
{
string address = this.Address.Render(logEvent.LogEvent);
byte[] bytes = this.GetBytesToWrite(logEvent.LogEvent);
if (this.KeepConnection)
{
NetworkSender sender = this.GetCachedNetworkSender(address);
this.ChunkedSend(
sender,
bytes,
ex =>
{
if (ex != null)
{
InternalLogger.Error("Error when sending {0}", ex);
this.ReleaseCachedConnection(sender);
}
logEvent.Continuation(ex);
});
}
else
{
var sender = this.SenderFactory.Create(address);
sender.Initialize();
lock (this.openNetworkSenders)
{
this.openNetworkSenders.Add(sender);
this.ChunkedSend(
sender,
bytes,
ex =>
{
lock (this.openNetworkSenders)
{
this.openNetworkSenders.Remove(sender);
}
if (ex != null)
{
InternalLogger.Error("Error when sending {0}", ex);
}
sender.Close(ex2 => { });
logEvent.Continuation(ex);
});
}
}
}