Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bugfix: Can't update EventLog's Source property #1548

Merged
merged 3 commits into from
Jul 23, 2016
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion src/NLog/Targets/EventLogTarget.cs
Original file line number Diff line number Diff line change
Expand Up @@ -349,7 +349,9 @@ internal string GetFixedSource()
/// <returns></returns>
private EventLog GetEventLog(LogEventInfo logEvent)
{
return eventLogInstance ?? (eventLogInstance = new EventLog(this.Log, this.MachineName, this.Source.Render(logEvent)));
if (eventLogInstance?.Source == this.Source.Render(logEvent))
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In worst case we're rendering the source two times?

Also there is an (implicit) two times check on null and we need to check also log and machine?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do you mean something like this?

if (eventLogInstance?.Source == this.Source.Render(logEvent) && eventLogInstance?.Log == this.Log && eventLogInstance?.MachineName == this.MachineName)
                return eventLogInstance ?? (eventLogInstance = new EventLog(this.Log, this.MachineName, this.Source.Render(logEvent)));
            return eventLogInstance = new EventLog(this.Log, this.MachineName, this.Source.Render(logEvent));

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Think it fixes the issue (hard to read on mobile) but Its still double checking null (?. and ==), which is a bit sloppy

return eventLogInstance ?? (eventLogInstance = new EventLog(this.Log, this.MachineName, this.Source.Render(logEvent)));
return eventLogInstance = new EventLog(this.Log, this.MachineName, this.Source.Render(logEvent));
}

/// <summary>
Expand Down
48 changes: 48 additions & 0 deletions tests/NLog.UnitTests/Targets/EventLogTargetTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -376,6 +376,44 @@ public void WriteEventLogEntryLargerThanMaxMessageLengthWithOverflowDiscard_TheM
Assert.False(wasWritten);
}


[Fact]
public void WriteEventLogEntryWithDynamicSource()
{
const int maxMessageLength = 10;
string expectedMessage = string.Join("", Enumerable.Repeat("a", maxMessageLength));

var target = CreateEventLogTarget(null, "NLog.UnitTests" + Guid.NewGuid().ToString("N"), EventLogTargetOverflowAction.Split, maxMessageLength);
target.Layout = new SimpleLayout("${message}");
target.Source = new SimpleLayout("${event-properties:item=DynamicSource}");
SimpleConfigurator.ConfigureForTargetLogging(target, LogLevel.Trace);

var logger = LogManager.GetLogger("WriteEventLogEntry");

var sourceName = "NLog.UnitTests" + Guid.NewGuid().ToString("N");
var logEvent = CreateLogEventWithDynamicSource(expectedMessage, LogLevel.Trace, "DynamicSource", sourceName);

logger.Log(logEvent);

var eventLog = new EventLog(target.Log);
var entries = GetEventRecords(eventLog.Log).ToList();

entries = entries.Where(a => a.ProviderName == sourceName).ToList();
Assert.Equal(1, entries.Count);
AssertWrittenMessage(entries, expectedMessage);

sourceName = "NLog.UnitTests" + Guid.NewGuid().ToString("N");
expectedMessage = string.Join("", Enumerable.Repeat("b", maxMessageLength));

logEvent = CreateLogEventWithDynamicSource(expectedMessage, LogLevel.Trace, "DynamicSource", sourceName);
logger.Log(logEvent);

entries = GetEventRecords(eventLog.Log).ToList();
entries = entries.Where(a => a.ProviderName == sourceName).ToList();
Assert.Equal(1, entries.Count);
AssertWrittenMessage(entries, expectedMessage);
}

private static IEnumerable<EventRecord> Write(LogLevel logLevel, EventLogEntryType expectedEventLogEntryType, string logMessage, Layout entryType = null, EventLogTargetOverflowAction overflowAction = EventLogTargetOverflowAction.Truncate, int maxMessageLength = 16384)
{
var target = CreateEventLogTarget(entryType, "NLog.UnitTests" + Guid.NewGuid().ToString("N"), overflowAction, maxMessageLength);
Expand Down Expand Up @@ -433,6 +471,16 @@ private static EventLogTarget CreateEventLogTarget(Layout entryType, string sour
return target;
}

private LogEventInfo CreateLogEventWithDynamicSource(string message, LogLevel level, string propertyKey, string proertyValue)
{
var logEvent = new LogEventInfo();
logEvent.Message = message;
logEvent.Level = level;
logEvent.Properties[propertyKey] = proertyValue;

return logEvent;
}

private static IEnumerable<EventRecord> GetEventRecords(string logName)
{
var query = new EventLogQuery(logName, PathType.LogName) { ReverseDirection = true };
Expand Down