如何加密log4net日志文件
有什么方法可以加密或保护 log4net 输出吗?
Is there any way to encrypt or secure log4net output?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
有什么方法可以加密或保护 log4net 输出吗?
Is there any way to encrypt or secure log4net output?
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
接受
或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
发布评论
评论(4)
我假设您想加密日志的输出。 在这种情况下,您将需要编写自己的 Appender 来处理加密。 我建议弄清楚您打算在不加密的情况下使用哪种输出机制(即 FileAppender、EventLogAppender 等),然后扩展该 Appender 并覆盖实际写出输出的功能。
有关附加程序的参考文档可以在此处找到。
例如,使用 EncryptedFileAppender 扩展 FileAppender 并覆盖/实现您需要的成员,以便挂钩文件写入。
或者,您可以从 IAppender 扩展并完全从头开始创建附加程序。 这将为您提供更多控制权,但如果您只想加密有效负载,则可能需要更多工作。
I'm assuming you want to encrypt the log's output. In that case you will need to write your own Appender which can handle the encryption. I would suggest figuring out what output mechanism you intend to use without encryption (i.e. FileAppender, EventLogAppender, etc.), and then extend that Appender and override the functionality that actually writes out the output.
The reference documentation on the appenders can be found here.
For instance, extend the FileAppender with an EncryptedFileAppender and override/implement the members you need to in order to hook into the file writing.
Alternatively, you could extend from IAppender and create an appender completely from scratch. That would give you more control, but might require more work if all you're trying to do is encrypt your payload.
如果您试图阻止用户通过网络阅读该日志,您可以将写入日志记录的文件名更改为您不允许网站提供服务的扩展名。 这样,用户就无法猜测您的日志文件并通过网络访问它。
如果您试图阻止登录到服务器本身的用户查看文件的内容,您可以使用权限控制来锁定文件,以便只有特定管理员组中的用户才能查看内容。
或者,您可以登录到数据库,这样就根本没有需要保护的文件。
If you are attempting to prevent users from reading it over the web, you can change the filename you are writing the log records in to an extension which you do not allow to be served by your website. This way, users cannot guess at your log file and access it over the web.
If you are trying to prevent users logged on to the server itself from viewing the contents of the file, you could use permission control to lock the file down so that only users in specific administrator groups could view the contents.
Alternatively, you can log to the database so that there is no file that needs to be secured at all.
没有现成的加密支持。 因此,正如其他人在这里所说的那样,您必须自己实现。
也就是说,我建议子类化 ForwardingAppender 来执行以下操作加密。 这基本上可以让您将您的附加程序“放在”您选择实际写入磁盘的任何标准附加程序“前面”。
There's no out-of-the-box support for encryption. So as others have stated here, you will have to implement that yourself.
That said, I would suggest subclassing a ForwardingAppender to do the encryption. This will basically let you put your appender "in front of" whatever standard appender you would choose to do the actual writing to disk.
我意识到这个答案是在原始发布日期几年后出现的,但在面临同样的问题后,我决定创建一个开源包来完成这项工作:Log4Net Message Encryptor
源代码可以在 GitHub
并且可以从 NuGet
I realise that this answer comes a few years after the original post date, but after facing the same problem I decided to create an Open source package to do just this job: Log4Net Message Encryptor
The source code can be found on GitHub
And the package can be downloaded from NuGet