Log4Net RollingFileAppender 无法作为网络服务删除文件
Log4Net RollingFileAppender Can't Delete Files as Network Service
我让 log4net 每天在 Windows 2008 服务器上创建一个日志和调试文件,作为作为网络服务运行的 ASP.MVC 应用程序的一部分。它可以很好地创建日志,但也有一个运行的清理操作,但这在删除文件时存在权限问题。我得到的错误是:
Access to the path 'C:\inetpub\Testing\MyApplication\log\Error-2016.08.22.log' is denied.
如果我更改文件的权限以便网络服务具有完全控制权,那么应用程序可以正常删除它。
代码如下:
private void ConfigLog4Net(int maxAgeInDays)
{
var sFilename = AppDomain.CurrentDomain.BaseDirectory + @"\Log4Net.config";
log4net.Config.XmlConfigurator.ConfigureAndWatch(
new System.IO.FileInfo(sFilename));
Log.InfoFormat("Log4Net Config - {0}", sFilename);
var repository = LogManager.GetRepository() as Hierarchy;
if (repository != null)
{
var appenders = repository.GetAppenders();
if (appenders != null)
{
foreach (var appender in appenders)
{
if (appender is RollingFileAppender)
{
var rollingFileAppender = appender as RollingFileAppender;
CleanupLogs(rollingFileAppender, maxAgeInDays);
}
}
}
}
}
private void CleanupLogs(RollingFileAppender appender, int maxAgeInDays)
{
if (!File.Exists(appender.File)) return;
var datePatternBits = appender.DatePattern.Split(new char[]{'\''}, StringSplitOptions.None);
if (datePatternBits.Count() != 5 || datePatternBits[0].Length > 0 || datePatternBits[4].Length > 0)
throw new ApplicationException(
string.Format(
"Log4Net RollingFileAppender ({0} DatePattern unexpected format. Expected \"\'xxx\'date\'eee\'\" {1},{2},{3}",
appender.Name, datePatternBits.Count(), datePatternBits[0].Length, datePatternBits[4].Length));
List<string> logPatternsToKeep = new List<string>();
for (var i = 0; i <= maxAgeInDays; i++)
logPatternsToKeep.Add(DateTime.Now.AddDays(-i).ToString(appender.DatePattern));
FileInfo fileInfo = new FileInfo(appender.File);
var searchString = string.Format("{0}*{1}", datePatternBits[1], datePatternBits[3]);
var folderFiles =
fileInfo.Directory.GetFiles(searchString);
var logFiles = folderFiles
.Where(x => logPatternsToKeep.All(y => !x.Name.Contains(y) && x.Name != fileInfo.Name));
foreach (var log in logFiles)
if (File.Exists(log.FullName)) File.Delete(log.FullName);
}
是否需要对网络服务权限执行某些操作,或者我是否需要更改代码以自动允许网络服务删除文件?
网络服务没有删除日志目录中文件的权限。
您需要授予网络服务权限才能删除 C:\inetpub\Testing\MyApplication\log\ 目录中的文件(不是其中的文件,而是目录本身)。
我让 log4net 每天在 Windows 2008 服务器上创建一个日志和调试文件,作为作为网络服务运行的 ASP.MVC 应用程序的一部分。它可以很好地创建日志,但也有一个运行的清理操作,但这在删除文件时存在权限问题。我得到的错误是:
Access to the path 'C:\inetpub\Testing\MyApplication\log\Error-2016.08.22.log' is denied.
如果我更改文件的权限以便网络服务具有完全控制权,那么应用程序可以正常删除它。
代码如下:
private void ConfigLog4Net(int maxAgeInDays)
{
var sFilename = AppDomain.CurrentDomain.BaseDirectory + @"\Log4Net.config";
log4net.Config.XmlConfigurator.ConfigureAndWatch(
new System.IO.FileInfo(sFilename));
Log.InfoFormat("Log4Net Config - {0}", sFilename);
var repository = LogManager.GetRepository() as Hierarchy;
if (repository != null)
{
var appenders = repository.GetAppenders();
if (appenders != null)
{
foreach (var appender in appenders)
{
if (appender is RollingFileAppender)
{
var rollingFileAppender = appender as RollingFileAppender;
CleanupLogs(rollingFileAppender, maxAgeInDays);
}
}
}
}
}
private void CleanupLogs(RollingFileAppender appender, int maxAgeInDays)
{
if (!File.Exists(appender.File)) return;
var datePatternBits = appender.DatePattern.Split(new char[]{'\''}, StringSplitOptions.None);
if (datePatternBits.Count() != 5 || datePatternBits[0].Length > 0 || datePatternBits[4].Length > 0)
throw new ApplicationException(
string.Format(
"Log4Net RollingFileAppender ({0} DatePattern unexpected format. Expected \"\'xxx\'date\'eee\'\" {1},{2},{3}",
appender.Name, datePatternBits.Count(), datePatternBits[0].Length, datePatternBits[4].Length));
List<string> logPatternsToKeep = new List<string>();
for (var i = 0; i <= maxAgeInDays; i++)
logPatternsToKeep.Add(DateTime.Now.AddDays(-i).ToString(appender.DatePattern));
FileInfo fileInfo = new FileInfo(appender.File);
var searchString = string.Format("{0}*{1}", datePatternBits[1], datePatternBits[3]);
var folderFiles =
fileInfo.Directory.GetFiles(searchString);
var logFiles = folderFiles
.Where(x => logPatternsToKeep.All(y => !x.Name.Contains(y) && x.Name != fileInfo.Name));
foreach (var log in logFiles)
if (File.Exists(log.FullName)) File.Delete(log.FullName);
}
是否需要对网络服务权限执行某些操作,或者我是否需要更改代码以自动允许网络服务删除文件?
网络服务没有删除日志目录中文件的权限。
您需要授予网络服务权限才能删除 C:\inetpub\Testing\MyApplication\log\ 目录中的文件(不是其中的文件,而是目录本身)。