-
Notifications
You must be signed in to change notification settings - Fork 125
Get currently active logging file? #129
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
Comments
I guess you could scan the folder and pick out the file with the latest "last modified" time? Otherwise, I'm not sure there a good way to do this 🤔. You could use public class MyHooks : FileLifecycleHooks
{
private readonly MyDep dep;
public MyHooks(MyDep dep)
{
this.dep = dep;
}
public override Stream OnFileOpened(Stream underlyingStream, Encoding _)
{
var filename = ((FileStream)underlyingStream).Name;
myDep.Notify(filename);
return base.OnFileOpened(underlyingStream, encoding);
}
} BUT, this will only work if your logger doesn't have @nblumhardt do you see any other way to get the current filename? I'm wondering too if |
Thanks for the quick reply! |
Based on the answer of @cocowalla I created this solution. Not super clean, as it uses reflection, but should be safe internal class LogFileLifecycleHooks : FileLifecycleHooks
{
public Action<string> FileNameChanged { get; set; }
public LogFileLifecycleHooks(Action<string> fileNameChanged)
{
FileNameChanged = fileNameChanged;
}
public override Stream OnFileOpened(Stream underlyingStream, Encoding encoding)
{
var streamType = underlyingStream.GetType();
if (streamType.FullName == "Serilog.Sinks.File.WriteCountingStream")
{
var streamField = streamType.GetField("_stream", BindingFlags.NonPublic | BindingFlags.Instance);
underlyingStream = streamField.GetValue(underlyingStream) as Stream;
}
if (underlyingStream is FileStream stream)
{
string filename = stream.Name;
FileNameChanged?.Invoke(filename);
}
return base.OnFileOpened(underlyingStream, encoding);
}
} |
@Khaos66 There's no need to user reflection if you're using a recent version of this sink. Since Serilog.Sinks.File v5.0.0 (June 2021), internal class CaptureFilePathHook : FileLifecycleHooks
{
public string? Path { get; private set; }
public override Stream OnFileOpened(string path, Stream underlyingStream, Encoding encoding)
{
Path = path;
return base.OnFileOpened(path, underlyingStream, encoding);
}
} |
Hi, I am struggling to understand the usage in order to get the active log file path. This is how I tried and the var logFilePath = Path.Combine(Path.GetTempPath(), "__MyApp_Log..Log");
var filePathHook = new CaptureFilePathHook();
Log.Logger = new LoggerConfiguration()
.Enrich.FromLogContext()
.MinimumLevel.ControlledBy(LoggingLevelSwitch) // Default's to Information
.WriteTo.Console(outputTemplate: logTemplate, theme: AnsiConsoleTheme.Code)
.WriteTo.File(
logFilePath,
rollingInterval: RollingInterval.Day,
retainedFileCountLimit: 7,
flushToDiskInterval: TimeSpan.FromSeconds(5),
outputTemplate: logTemplate,
hooks: filePathHook)
.CreateLogger();
LoggingLevelSwitch.MinimumLevel = logEventLevel;
var logFIleFullPath = filePathHook.Path; // <---- is null
// .....
class CaptureFilePathHook : FileLifecycleHooks
{
public string? Path { get; private set; }
public override Stream OnFileOpened(string path, Stream underlyingStream, Encoding encoding)
{
Path = path;
return base.OnFileOpened(path, underlyingStream, encoding);
}
} |
My code which is similar to your works in returning a path. The only difference that I could see is that my code goes to a type of
|
@worthapenny This is because you're not writing anything to the log, and the hook was not yet initialized... You can put a breakpoint the Log.Information("Hello");
var logFileFullPath = filePathHook.Path; // <----should have the path of the current file |
What if I need to access the filepath at a different place than where I defined the filePathHook? Can I get it from |
@emilaz Make sure the file path hook updates a shared variable that you can access from the place you need in your app. |
So, I have managed to get this example working, but you mention sharing the path through shared variable... how would I do this? I am creating the logger by reading a configuration file, and that works fine, but I need to be able to the read the path from my application. |
@hhgm Create a public property on the hook that you can access to retrieve the path. Like the example above #129 (comment) |
How can I get the path if the file logger is configuration is red from appsettings?
|
I know this has been here a while.... I'm trying to get the hooks to work. I'm getting an object reference is required error in the ide. What I'm trying to do is expose the value as part of a COM object Property. What is happening is that I do see the hooks getting called and the data I seek being set, however I cannot access it when looking at locals window. Which of course means I can't pass it to my holding property. Here is the code:
Logger open method:
I call as follows:
As I say I can see the LogFileFQN getting set when I debug but the access to it is landlocked. Can anyone see what I'm missing? Update: After digging and digging I figured it out.
Then in the StartLogger I added:
Now as someone else noted here, you must of course have actually written an item to the log file.
I hope this helps others and perhaps one day we might actually see a better way across all file sinks the actual log filename be exposed as a standard feature. |
Hi,
is there a way to get the currently active logging File?
I want to expose it via HTTP (don't ask...) and would like to know which file I need to expose.
The text was updated successfully, but these errors were encountered: