NOTE: I realize that this question is similar to this SO thread, but I am asking about a specific case here, so please don't close this question as a duplicate of that one.
I'm trying to make my C# more functional, and have been reading Functional Programming in C# by Enrico Buonanno. One of the techniques he shows is a Try
class, that allows you to handle exceptions gracefully. You can see the code for the Try
class here.
This is all very neat, but in a real app, I would want to log exceptions. As his class is static, I don't see a way to do it. As explained in the linked SO question above (amongst others), injecting into a static class either can't be done or isn't a good idea.
The only options I can see are:
Requiring the consuming code to create an instance of the
Try
class (not currently possible with his code), as opposed to calling the static method. This makes the code a lot less cleanAdding a static
ILogger
property to theTry
class, and setting it before using the class. I don't want to do this, as theTry
class will be used on both the client and server in my application, and the logger would be different in the two casesAdding an
ILogger
parameter to the class methods. I don't like this for the same reasons as in 1.Accessing the dependency resolver inside the
try
class and getting an instance of the logger. This would mean that the code that usesTry
would also end up logging to the file, which is not a good idea.
Anyone any suggestions? The approach seems so clean and neat, but it also seems to be missing an obvious basic function.