Comments (17)
That's great news! Looking forward to your decision.
BTW I updated in a similar way SerilogLoggerProvider.Enrich()
, specifically the following line
var property = propertyFactory.CreateProperty(keyValue.Key, keyValue.Value);
That allows me to write code like:
var foo = ... // some object
var myLogValues = new MyLogValues(); // Implements ILogValues
myLogValues.Add("@Foo", foo); // Use destructuring parameter
using (logger.BeginScope(myLogValues))
{
logger.LogInformation("Some message");
}
This way I can attach destructured objects to my logging messages in cases where I don't want them to be part of the message template (essentially get a similar behavior as with ForContext()
).
from serilog-extensions-logging.
Doing some build work so dropped this change in; @alecor191 did you find the null-check necessary in:
if ((property.Key != null) && (property.Key.StartsWith("@")))
? I left it off since I think it'd probably violate the intentions if not the contract of ILogValues
to return null for the key, but if you hit it in practice it'd be great to know.
Thanks again for the suggestion!
from serilog-extensions-logging.
Thanks for the feedback. M.F.L doesn't actually support Serilog directly; the {Named}
argument support is provided natively by M.F.L and so only the features it provides work with its pipeline.
Are you writing a web application, or middleware? If you are building an app, using Serilog directly is the best bet. There are options to get structured data through M.F.L but they're a bit more involved, happy to help.
from serilog-extensions-logging.
Hi @nblumhardt. Thanks for your reply. I'm working on both middleware and web apps and I would like my assemblies to all depend on the same logging framework (M.F.L.).
I understand that Serilog comes with some additional features that M.F.L doesn't provide. About the specific limitation mentioned above, I did a quick POC: Essentially replace the following line in SerilogLogger.cs (where it iterates through structure.GetValues())
logger = logger.ForContext(property.Key, property.Value);
with the following
if ((property.Key != null) && (property.Key.StartsWith("@")))
{
logger = logger.ForContext(property.Key.Substring(1), property.Value, true);
}
else
{
logger = logger.ForContext(property.Key, property.Value, false);
}
This will result in the following behavior: When a '@' destructing argument is used in a message template, it will destruct the provided object (like when used directly with Serilog APIs)
What are your thoughts? Would this be something you could consider adding to the project as well?
from serilog-extensions-logging.
Hi @alecor191 - that's very interesting - thanks for digging in deeper!
Definitely a strong proposal; I can't see too many issues with the approach (perhaps a very slight performance hit in the general case caused by StartsWith()
), but let me think through it just a fraction more carefully.
Looking good though!
from serilog-extensions-logging.
@alecor191 Also at the moment you can achieve the same thing by doing:
using (logger.BeginScope("{@Foo1} {@Foo2} ignored", foo1, foo2))
{
...
}
'ignored' will not be logged.
from serilog-extensions-logging.
The destructuring feature in Serilog does look particularly useful, and it seems a shame for it not to become available with MFL.
So from a purist point of view, as MFL doesn't support the destructuring concept, it looks a little 'leaky' to have '@'s in the message format; switching to a different logging provider means you end up with lots of properties starting with an '@' that you didn't have previously.
My current project is a large ASP.NET 5 based product, where customers have the freedom to customise it in every way, so although we will ship with a default logging implementation (probably Serilog), our application code will only be dependent on MFL for all the obvious reasons.
At the moment, I think I am OK with going with the '@' approach. The only alternatives I can think of are less desirable, short of persuading Microsoft to support complex properties...
from serilog-extensions-logging.
@trevorreeves thanks for the notes!
@alecor191 I'd like to move forward on this incrementally; let's make the first change (support for @
in message templates) and see how we go with that. I'm less sure about the second suggestion, since although Serilog does provide destructuring for enriched properties, the API is semantically different (not based on @
template holes).
Would you be interested in creating a PR against dev
for the change?
from serilog-extensions-logging.
Hi @nblumhardt. Sorry for the late reply. I didn't run into a scenario where the null check would be necessary. I just added it in my prototype.
Thanks a lot for including this change!
from serilog-extensions-logging.
Thanks for the follow-up @alecor191 - cheers!
from serilog-extensions-logging.
Published: https://www.nuget.org/packages/Serilog.Framework.Logging/1.0.0-beta-4
from serilog-extensions-logging.
Hi @nblumhardt. I know this is a pretty old issue, but I wanted to see if there was any more thought given to possibly supporting destructuring of objects in BeginScope? As @alecor191 mentioned, it is handy in providing functionality similar to ForContext(). Or is there better way to do that within MEL?
Thanks!
from serilog-extensions-logging.
Hi @wlo023 - I think the MEL way to do it (which Serilog supports) is just to pass a dictionary:
BeginScope(new Dictionary<string,object>{["Foo"] = "Bar", ...})
Does this cover what you're thinking of?
Cheers!
from serilog-extensions-logging.
@nblumhardt - that does work for simple values (i.e. string, int, etc) but for complex objects, it just does a "ToString()" on the object when I really want it to destructure the object.
i.e.
using (BeginScope("{@user}", new User { FirstName = "John", LastName = "Smith"})
I would only get ".User" as the value of "user" in the log.
I think this is because in the SerilogLoggerScope.cs, the Enrich method has these lines:
var property = propertyFactory.CreateProperty(stateProperty.Key, stateProperty.Value);
logEvent.AddPropertyIfAbsent(property);
with destructureObjects always set to the default of false. I think a similar trick to what was suggested before with the messages and checking if the key starts with "@" and then passing the correct boolean value for destructureObjects would do it.
One of the overloads for BeginScope is:
public static IDisposable BeginScope(this ILogger logger, string messageFormat, params object[] args)
That is pretty similar to the API for logging a message, so to me, it would seem to make sense to support the destructuring operator in the messageFormat.
If you agree, I can volunteer to create a pull request with the changes and unit tests.
Thanks!
from serilog-extensions-logging.
I see, yes, we should accept @
in this overload.
using (BeginScope("{@user}", new User { FirstName = "John", LastName = "Smith"})
The corresponding LOC is:
PR happily accepted, thanks 👍
from serilog-extensions-logging.
Just a simple question, so in MEL, since there is no Serilog ForContext correlation concept the recommended approach is to use BeginScope.
from serilog-extensions-logging.
Correct - it seems that the closest analogue for ForContext is the BeginScope functionality
from serilog-extensions-logging.
Related Issues (20)
- AWSSeriLog not working with Maui Blazor App HOT 1
- No exceptions emitted when generated by faulty audit log configuration HOT 4
- Upgrade codebase HOT 2
- Serilog keeps throwing ArgumentNullException HOT 10
- Add API approval test
- Could not load file or assembly 'Microsoft.Extensions.Logging, Version=7.0.0.0, Culture=neutral, PublicKeyToken=adb9793829ddae60' HOT 2
- Serilog.Extensions.Logging 7.0.0 has a .NET 6.0 TFM with dependencies on 7.0 HOT 3
- Serilog.Extensions.Logging v7 .net6 requires v7 Microsoft.Extensions.Logging pkg
- Unit tests fail when run for .Net Framework HOT 2
- SerilogLogger.cs - Always calls formatter with the Exception parameter as null HOT 1
- SerilogLogger.cs: PrepareWrite Method not found Serilog.Events.LogEvent..ctor HOT 3
- Serilog .NET 8 Windows Service (worker) using BackgroundService Log file not found HOT 2
- Pass-through of `M.E.L.ILogger.BeginScope()` when using custom MEL `ILoggingProvider` as Serilog sink? HOT 5
- Avoid boxing of TState HOT 6
- M.E.L. Log category support exists but is non-obvious on how to include it in text sinks
- Support ActivityTrackingOptions to enrich logs with Activity tags and baggage HOT 5
- Collision with Microsoft.Extensions.Telemetry HOT 1
- `ISupportExternalScope` enrichment HOT 4
- String Quoting - global disable switch HOT 8
- Serilog ignores dotnet logging configuration HOT 2
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from serilog-extensions-logging.