-
Notifications
You must be signed in to change notification settings - Fork 1.4k
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
Can't update EventLog's Source property #1542
Comments
We render every event the filename. The see: /// <summary>
/// Writes the specified logging event to a file specified in the FileName
/// parameter.
/// </summary>
/// <param name="logEvent">The logging event.</param>
protected override void Write(LogEventInfo logEvent)
{
var fileName = Path.GetFullPath(GetCleanedFileName(logEvent));
byte[] bytes = this.GetBytesToWrite(logEvent);
ProcessLogEvent(logEvent, fileName, bytes);
}
private string GetCleanedFileName(LogEventInfo logEvent)
{
if (this.FileName == null)
{
return null;
}
return cachedCleanedFileNamed ?? CleanupInvalidFileNameChars(this.FileName.Render(logEvent));
} But are your really using the LoggerBase? I would recommend to write for LoggerBase to the |
The source is the problem, not the filename. Also I inherited from NLog.Logger. I just called MY class LoggerBase (exactly KAV.Tools.Logging.LoggerBase), since I'm going to inherit from it another time (it will be KAV.Tools.Logging.Logger : KAV.Tools.Logging.LoggerBase : NLog.Logger). "My" LoggerBase handles all the stuff I want to handle it and "my" Logger is only for special customizing in the specific project. |
Ow did misread it. Will check again |
indeed, the relevant code: /// <summary>
/// Get the eventlog to write to.
/// </summary>
/// <param name="logEvent">Event if the source needs to be rendered.</param>
/// <returns></returns>
private EventLog GetEventLog(LogEventInfo logEvent)
{
return eventLogInstance ?? (eventLogInstance = new EventLog(this.Log, this.MachineName, this.Source.Render(logEvent)));
} This is indeed a bug! |
Wow! I'm glad my first report actually was no "error" (on my side) ;) |
I'm a bit busy these days. Do you think you can fixed this bug and send a PR? |
ah... hm... never did such a thing and since I'm new to NLog. |
you have to change EventLogTarget.cs It's a relative easy target.
|
Hi! Sorry, I have no idea how to upload my patched EventLogTarget.cs It's a real simple thing: private EventLog GetEventLog(LogEventInfo logEvent)
{
if (eventLogInstance?.Source == this.Source.Render(logEvent))
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));
} Please copy/paste it and thank you for your amazing work! |
@ArchonMegalon Thanks. I will take a look at this. |
UPDATE: I am trying to get some Unit Tests around the fix. Once those are completed will send in a PR. |
Cool! |
PR: #1548 |
Fixed and online (NLog 4.3.6) Thanks @page-not-found ! |
KAV.Tools.Logging.1.0.0.21.zip
Hi!
My first report, so please be gentle... ;)
The important thing is happening in the LogInternal function of my custom (from NLog.Logger inherited) Loggerclass:
logEvent.Properties["MySource"] = useSource; <-- this is set per call
The second time the function is called (and useSource is different), of course the Properties are set, but the EventLogTargets SourceAttribute is not updated! It has always the value from the first call.
EventID is updated.
Type: Bug
NLog version: 4.3.5
Platform: .Net 4.5 (don't know what else - haven't tried it)
Current NLog config:
And here only the "helping Classes"...
In case of a BUG:
Currently the Source source="${event-properties:item=MySource}" is set only once - the first time.
I would have thought it would be set (or updated) in every logEvent.
Yes (a bit). Didn't find useful information in there (regarding my "problem").
No exception - it just uses the first Source. All Sources are registerd by the way...
Well - one can always "hardcode" the source in the EventLog or use special rules/filters for special targets. But that's not really confinement.
Don't think so - I just started using NLog in that way.
I guess... Since this is my first bug-report on github I'm sure willing to try...
I made a nuget-package out of this - would that be helpful?
The text was updated successfully, but these errors were encountered: