-
-
Notifications
You must be signed in to change notification settings - Fork 25
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
Make IRichTextBox public for Dependency Injection #18
Comments
Thanks @LazerFX for the suggestion and detailed scenario explanation. Conceptually I think your idea makes sense - i.e. Provide a way to delay providing the The I'm thinking a way forward could be to create a new factory-like interface that returns the public interface IRichTextBoxProvider
{
System.Windows.Controls.RichTextBox GetRichTextBox();
} This should allow for the scenario you described above... The only difference is the public API is more high level and less likely to change. Would that work? If Yes the next question is what to do with log messages that are written to the log during the start-up of the app, before the
or
or
|
Thanks for the quick response! I see what you mean - I can provide an For my scenario, I'm not fussed about the startup logging; however, I can understand that others would want those logged... I can see it getting tricky - if you've a nominally complex WPF app, you're going to have a massive volume of binding events logged if you've even slightly high levels of logging, and that will rapidly eat up cache space - you don't want people asking why suddenly there's a massive memory load during startup when they turn on logging to the |
Cool. Agreed that the caching part needs some more thought. Let's not get blocked by that then, we can start with a first version that has no buffering and discards the messages if the control is not available (i.e. whenever Feel free to send a PR at your convenience. |
Been looking through this - might take me a while to put a PR together for this; got pulled onto some things at work which means personal projects are slipped onto the back burner. Will close this issue as there's a plan if I want to go ahead on this, and say thanks for the support in deciding the best route :) |
No worries at all. I think others might be interested in this feature as well, so let's leave the issue open as |
I'm not a WPF programmer but I took the sample for the net core 5 winforms/WPF and incorporated it into an existing winform (csproj edited as with the code from WinFormsHostNet50Sample) that works with DI of Serliog. I was able to get the DI to the MainForm, from the program.cs. Here is what I did. I made a static public reference for the RichTextBox and the ElementHost in program.cs. I setup Serilog in the ConfigureServices(), along with using the Reference to the RIchTextBox and reading my other Serilog setting form my appsettings.json. In the MainForm, where I had placed the _richTextBoxPanel (see sample code for WinFormsHostNet50Sample), after the InitializeComponent() line, I added
So the partial header of MainForm.cs is:
} I was able to use _logger.LogInformation from this point. All of the other classes have the ILogger Injection as Ilogger |
If I'm understanding you right, you basically made a global static singleton RichTextBox reference that's outside any DI framework? That's not Dependency Injection, that's a global variable. Which, if you're looking for a testable, maintainable environment is... very much a no-no; though I might be misunderstanding you're demonstration here, and I'll be honest I've not looked at the code yet. |
Thanks @KayakFisher205. Interesting hack, thanks for sharing! I will say though it's not exactly something I'd recommend as seems too early in the process for creating UI controls, but if it works for you. @LazerFX That's it. The code sample holds a global static instance of a detached RichTextBox control that is created at the start and later shared with the view that will display it. That said, it would technically be possible to avoid using globals by registering the RichTextBox instance with the container, and having it as a dependency of the view (resolved by the DI container) - still a hack and not ideal, but "testable" |
Agreed, I misspoke about DI. As you both said, it is definitely a hack and not good for production. Hopefully, it will be a catalyst for the really smart people to figure something out. |
I would like this feature as well. What also would be an option is to supply a Func to the WriteTo Extension method, so it can be resolved. |
@frankhommers I'm not sure a Either way, feel free to send a PR at your convenience. |
You can always add the RichTextBox as a 'named instance' to the service provider, that way the RTB will collect logs from the start, and you can retrieve the RTB in the constructor of the window/control you want to add it to. If named instances aren't supported in your DI framework, then you can create a wrapped object to hold the RTB. |
I'm writing an application (Purely for myself, I don't intend to use this in production or anything), but I'd like to use RichTextBox sink - saves me having to have a console or spin up a file dropper or something like that every time I want to see logger output. However, the application is written in .NET 5 WPF, using Dependency Injection throughout. Serilog is registered through DI.
The issue is that I cannot configure the Logger after application start because it uses the
UseSerilog
extension on theIHostBuilder
interface, but at that point the RichTextBox I want to target isn't instantiated.My solution would be simple - instantiate an IRichTextBox interface that can be set up as a
UseSingleton
value. Then, I can inject that into the targetWindow
, add theRichTextBox
itself to it, and do a pass-through from the interface to the actual controls on theRichTextBox
. I can't do this at the moment, as theIRichTextBox
interface and theWriteTo.RichTextBox
extension method that uses theIRichTextBox
isinternal sealed
.Before I go ahead and do this and put a PR together with the changes, I just thought I'd push this out for comment from the team working on this to see if anyone has any suggestions - maybe there's a better way of doing this?
The text was updated successfully, but these errors were encountered: