Skip to content

Commit

Permalink
MessageQueueTarget - Support SingleTransaction for sending message to…
Browse files Browse the repository at this point in the history
… transactional-queue (#5)
  • Loading branch information
snakefoot authored Sep 21, 2023
1 parent bfedeb1 commit 21b2de5
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 40 deletions.
2 changes: 1 addition & 1 deletion appveyor.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,6 @@ artifacts:
deploy:
- provider: NuGet
api_key:
secure: e+0IpLU3V1eXUsWCRjKGuyyeuLQYfFpv6BAoIihFQryuYZsOWVvxUBvQOC0dOL2n
secure: ACKSV1ixxNpO+2k8KvNDy6hd9QmR8lkQmKn773ZIIeVpG0ywYUhY4j8LcyykVR1a
on:
branch: master
2 changes: 1 addition & 1 deletion src/NLog.MSMQ/NLog.MSMQ.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
<TargetFrameworks Condition=" '$(TargetFrameworks)' == '' ">net46;net45;net35;netstandard2.0</TargetFrameworks>
<RootNamespace>NLog</RootNamespace>

<VersionPrefix>5.2.0</VersionPrefix>
<VersionPrefix>5.3.0</VersionPrefix>
<VersionSuffix></VersionSuffix>

<AssemblyVersion>5.0.0.0</AssemblyVersion>
Expand Down
81 changes: 45 additions & 36 deletions src/NLog.MSMQ/Targets/MessageQueueTarget.cs
Original file line number Diff line number Diff line change
Expand Up @@ -101,19 +101,19 @@ public MessageQueueTarget(string name) : this()
public Layout Queue { get; set; }

/// <summary>
/// Gets or sets the label to associate with each message.
/// Gets or sets a value indicating whether sending to a transactional queue using single-transaction-style.
/// </summary>
/// <remarks>
/// By default no label is associated.
/// </remarks>
/// <docgen category='Queue Options' order='10' />
public Layout Label { get; set; } = "NLog";
public bool SingleTransaction { get; set; }

/// <summary>
/// Gets or sets a value indicating whether to create the queue if it doesn't exists.
/// Gets or sets the label to associate with each message.
/// </summary>
/// <remarks>
/// By default no label is associated.
/// </remarks>
/// <docgen category='Queue Options' order='10' />
public bool CreateQueueIfNotExists { get; set; }
public Layout Label { get; set; }

/// <summary>
/// Gets or sets a value indicating whether to use recoverable messages (with guaranteed delivery).
Expand All @@ -129,7 +129,6 @@ public MessageQueueTarget(string name) : this()

/// <summary>
/// Gets or sets a value indicating whether to use the XML format when serializing message.
/// This will also disable creating queues.
/// </summary>
/// <docgen category='Layout Options' order='10' />
public bool UseXmlEncoding { get; set; }
Expand All @@ -138,8 +137,14 @@ public MessageQueueTarget(string name) : this()
/// Gets or sets a value indicating whether to check if a queue exists before writing to it.
/// </summary>
/// <docgen category='Layout Options' order='11' />
public bool CheckIfQueueExists { get; set; } = true;

public bool CheckIfQueueExists { get; set; }

/// <summary>
/// Gets or sets a value indicating whether to create the queue if it doesn't exists.
/// </summary>
/// <docgen category='Queue Options' order='10' />
public bool CreateQueueIfNotExists { get; set; }

internal MessageQueueProxy MessageQueueProxy { get; set; }

/// <summary>
Expand All @@ -149,27 +154,32 @@ public MessageQueueTarget(string name) : this()
/// <param name="logEvent">The logging event.</param>
protected override void Write(LogEventInfo logEvent)
{
if (Queue is null)
var queue = RenderLogEvent(Queue, logEvent);
if (string.IsNullOrEmpty(queue))
{
Common.InternalLogger.Debug("MessageQueueTarget: Failed sending message, because queue name is empty.");
return;
}

var queue = Queue.Render(logEvent);

if (CheckIfQueueExists && !IsFormatNameSyntax(queue) && !MessageQueueProxy.Exists(queue))
if (CheckIfQueueExists || CreateQueueIfNotExists)
{
if (CreateQueueIfNotExists)
{
MessageQueueProxy.Create(queue);
}
else
if (!IsFormatNameSyntax(queue) && !MessageQueueProxy.Exists(queue))
{
return;
if (CreateQueueIfNotExists)
{
Common.InternalLogger.Debug("MessageQueueTarget: Creating queue: {0}", queue);
MessageQueueProxy.Create(queue, SingleTransaction);
}
else
{
Common.InternalLogger.Debug("MessageQueueTarget: Skip sending message, because non-existing queue: {0}", queue);
return;
}
}
}

var msg = PrepareMessage(logEvent);
MessageQueueProxy.Send(queue, msg);
MessageQueueProxy.Send(queue, msg, SingleTransaction);
}

/// <summary>
Expand All @@ -185,22 +195,23 @@ protected override void Write(LogEventInfo logEvent)
protected virtual Message PrepareMessage(LogEventInfo logEvent)
{
var msg = new Message();
if (Label != null)
var label = RenderLogEvent(Label, logEvent);
if (!string.IsNullOrEmpty(label))
{
msg.Label = Label.Render(logEvent);
msg.Label = label;
}

msg.Recoverable = Recoverable;
msg.Priority = DefaultMessagePriority;

var body = RenderLogEvent(Layout, logEvent);
if (UseXmlEncoding)
{
msg.Body = Layout.Render(logEvent);
msg.Body = body;
}
else
{
var dataBytes = Encoding.GetBytes(Layout.Render(logEvent));

var dataBytes = Encoding.GetBytes(body);
msg.BodyStream.Write(dataBytes, 0, dataBytes.Length);
}

Expand All @@ -220,21 +231,19 @@ public virtual bool Exists(string queue)
return MessageQueue.Exists(queue);
}

public virtual void Create(string queue)
public virtual void Create(string queue, bool singleTransaction)
{
MessageQueue.Create(queue);
MessageQueue.Create(queue, singleTransaction);
}

public virtual void Send(string queue, Message message)
public virtual void Send(string queue, Message message, bool singleTransaction)
{
if (message is null)
{
return;
}

using (var mq = new MessageQueue(queue))
using (var mq = new MessageQueue(queue, QueueAccessMode.Send))
{
mq.Send(message);
if (singleTransaction)
mq.Send(message, MessageQueueTransactionType.Single);
else
mq.Send(message);
}
}
}
Expand Down
4 changes: 2 additions & 2 deletions tests/NLog.MSMQ.Tests/MessageQueueTargetTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -193,12 +193,12 @@ public override bool Exists(string queue)
return QueueExists;
}

public override void Create(string queue)
public override void Create(string queue, bool singleTransaction)
{
QueueCreated = true;
}

public override void Send(string queue, Message message)
public override void Send(string queue, Message message, bool singleTransaction)
{
SentMessages.Add(message);
}
Expand Down

0 comments on commit 21b2de5

Please sign in to comment.