带有 log4Net 的 .NET Web 部署工具:不覆盖日志

发布于 2024-11-08 00:16:28 字数 1377 浏览 4 评论 0 原文

我正在使用 Web 部署工具构建 MVC 站点并将其从 VS 2010 发布到运行 IIS 的服务器。我还将 log4net 日志记录到我要部署到的 Web 应用程序根目录之外的子目录中。我已经弄清楚如何在该目录上使用此工具进行部署时保持写权限完整,但现在我遇到了一个问题,即我不想在部署时丢失日志,而且部署失败,因为日志log4net 正在使用的文件“被另一个进程使用”(大概是 w3wp)并且不会让部署继续。

因此,出于审计目的,我想保留日志文件而不是删除或覆盖它们。有没有办法在 Web 部署工具的范围内做到这一点?

编辑:这是 Web.Config 中 log4net 配置的适用位。

<?xml version="1.0" encoding="utf-8"?>
<configuration>
  <configSections>
    <section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler,Log4net" />
  </configSections>
  <log4net>
    <appender name="RollingLog" type="log4net.Appender.RollingFileAppender">
      <param name="File" value="Logs\Log.txt" />
      <param name="AppendToFile" value="true" />
      <rollingStyle value="Composite" />
      <maxSizeRollBackups value="20" />
      <maximumFileSize value="10MB" />
      <datePattern value="yyyyMMdd" />
      <staticLogFileName value="true" />
      <layout type="log4net.Layout.PatternLayout">
        <conversionPattern value="{%level}%date{MM/dd HH:mm:ss} - %message%newline" />
      </layout>
    </appender>
    <root>
      <level value="DEBUG" />
      <appender-ref ref="RollingLog" />
    </root>
  </log4net>
</configuration>

I'm using the Web Deployment Tool to build and release an MVC site from VS 2010 to a server running IIS. I also have log4net logging to a subdirectory off of the root of the web application I'm deploying to. I already figured out how to keep write permissions intact when deploying with this tool on that directory, but now I'm running into the problem that I'd rather not lose the logs when deploying, and also, the deploy is failing because the log file that log4net is using is "used by another process" (presumably w3wp) and won't let the deploy continue.

So, I'd like to preserve the log files and not delete or overwrite them, for auditing purposes. Is there a way to do that within the confines of the Web Deployment Tool?

EDIT: Here's the applicable bits of the log4net configuration, in Web.Config.

<?xml version="1.0" encoding="utf-8"?>
<configuration>
  <configSections>
    <section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler,Log4net" />
  </configSections>
  <log4net>
    <appender name="RollingLog" type="log4net.Appender.RollingFileAppender">
      <param name="File" value="Logs\Log.txt" />
      <param name="AppendToFile" value="true" />
      <rollingStyle value="Composite" />
      <maxSizeRollBackups value="20" />
      <maximumFileSize value="10MB" />
      <datePattern value="yyyyMMdd" />
      <staticLogFileName value="true" />
      <layout type="log4net.Layout.PatternLayout">
        <conversionPattern value="{%level}%date{MM/dd HH:mm:ss} - %message%newline" />
      </layout>
    </appender>
    <root>
      <level value="DEBUG" />
      <appender-ref ref="RollingLog" />
    </root>
  </log4net>
</configuration>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

无法言说的痛 2024-11-15 00:16:28

通过四处寻找找到了它:当您调用预打包的部署脚本时,您可以在命令中添加一个“skip”参数。为此,您必须使用常规的旧 CMD 提示符; Powershell 疯狂的引号转义使得它几乎不可能正确,所以我放弃了。无论如何,这是我得出的最终结果:

.\MyProject.deploy.cmd /Y /M:MyServerName "-skip:skipAction=Delete,objectName=filePath,absolutePath=Logs"

“MyProject.deploy.cmd”是预打包部署命令的名称,“MyServerName”是我要部署到的服务器的名称,“Logs”是预打包部署命令的名称。我想跳过的文件夹。该命令似乎不理会 Logs 目录并部署其他重要的内容。

我开始磨练事物的来源:http://blogs.iis.net/msdeploy/archive/2009/04/23/what-has-changed-about-skip-replace-rules-in-rc.aspx

Found it by hunting around: there is a "skip" parameter you can tack on to the command when you call the pre-packaged deploy script. You HAVE to use a regular old CMD prompt for this; Powershell's crazy escaping of quotes makes it near-impossible to get right, so I gave up. Anyway, here's the end result I came up with:

.\MyProject.deploy.cmd /Y /M:MyServerName "-skip:skipAction=Delete,objectName=filePath,absolutePath=Logs"

"MyProject.deploy.cmd" being the name of the prepackaged deploy command, "MyServerName" being the name of the server I was deploying to, and "Logs" being the name of the folder I wanted to skip. This command seems to leave alone that Logs directory and deploy anything else that matters.

Source where I started to hone in on things: http://blogs.iis.net/msdeploy/archive/2009/04/23/what-has-changed-about-skip-replace-rules-in-rc.aspx

穿越时光隧道 2024-11-15 00:16:28

将附加程序的锁定模型设置为最小锁定,一切都会好起来的:

<appender name="RollingLog" type="log4net.Appender.RollingFileAppender">
  ...
  <lockingModel type="log4net.Appender.FileAppender+MinimalLock" />
  ...
</appender>

log4net.Appender.FileAppender(见备注)

Set locking model of your appender to minimal lock and everything will be fine:

<appender name="RollingLog" type="log4net.Appender.RollingFileAppender">
  ...
  <lockingModel type="log4net.Appender.FileAppender+MinimalLock" />
  ...
</appender>

log4net.Appender.FileAppender (see remarks)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文