Skip to content

Allow user to execute custom action on deprecated files (ex. move or send to different server) #106

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

Closed
nogard111 opened this issue Jul 5, 2019 · 5 comments

Comments

@nogard111
Copy link

Can we allow different action on obsolete files ?
I think it would be beneficial in situation when old logs should be archived by moving to different folder or different server (dedicated only for logs)

/// Action that should be preformed on obsolete(old) log files
public static Action<string> OnObsoleteFile = null;

Inside remove loop:

          foreach (var obsolete in toRemove)
            {
                var fullPath = Path.Combine(_roller.LogFileDirectory, obsolete);
                try
                {
                    if(OnObsoleteFile!= null)
                        OnObsoleteFile.Invoke(fullPath);
                    else
                    {
                        System.IO.File.Delete(fullPath);
                    }
                }
                catch (Exception ex)
                {
                    SelfLog.WriteLine("Error {0} while processing obsolete file {1}", ex, fullPath);
                }
            }

It is more like example, not working code :)

@cocowalla
Copy link
Contributor

It's not exactly what you describe, but FileLifecycleHooks (available in the preview version) should cover you here.

It allows you to hook in before a new log file is opened, so you can do things like wrap the output stream in a compressing or encrypting stream, add a custom header etc. But at this point you could also scan the log folder for old files that you want to ship, compress, sign,encrypt, delete or whatever.

You can see an example implementation at /cocowalla/serilog-sinks-file-header.

@nblumhardt
Copy link
Member

Sounds like a great candidate for an OnFileRemoving() hook. Interested in helping to push this through, if you're keen to PR it, @nogard111 ?

@cocowalla
Copy link
Contributor

@nblumhardt presumably this would only apply to RollingFileSink?

Do we want the presence of an OnFileRemoving() hook to mean "the caller will completely handle retention, Serilog does nothing", or "the caller will do something, then Serilog is responsible for deleting the file"?

In terms of API, we could have:
public abstract void OnFileRemoving(string filename);

or maybe
public abstract bool OnFileRemoving(string filename);

The idea with the latter being that the bool signifies whether Serilog should continue to delete the file as normal.

nogard111 pushed a commit to nogard111/serilog-sinks-file that referenced this issue Jul 8, 2019
@nogard111
Copy link
Author

I put PR , It you see some improvements/issues please let me know

nblumhardt added a commit that referenced this issue Jul 16, 2019
@cocowalla
Copy link
Contributor

Just a note to say there is a new FileLifecycleHooks-based plugin available that uses this OnFileDeleting hook to archive obsolete files, serilog-sinks-file-archive

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants