wwwroot 之外的 Elmah logPath

发布于 2024-11-27 08:27:59 字数 190 浏览 2 评论 0原文

我们正在尝试部署我们的项目,但无法让 elmah 在 wwwroot 之外创建 xml 日志。它当前正在记录到 inetpub{site}\wwwroot\App_Data,因为这是唯一可行的路径。我们希望将其记录到我们的 inetpub{site}\logs 文件夹中。有什么想法让它发挥作用吗?我们已经尝试过 ..\ 和 ../ 文件夹路径,但它们似乎不起作用。

We're trying to deploy our project but we can't get elmah to create the xml logs outsite the wwwroot. It's currently logging to inetpub{site}\wwwroot\App_Data because that's the only path that seemed to work. We would like to have it logging to our inetpub{site}\logs folder. Any ideas on getting this to work? We've already tried ..\ and ../ paths to the folder but they didn't seem to work.

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

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

发布评论

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

评论(4

往事风中埋 2024-12-04 08:28:00

有什么想法可以让它发挥作用吗?

您可以提供日志文件的绝对路径,但应确保配置为在 IIS 下运行 Web 应用程序的帐户具有对此文件夹的写入权限,以便它可以在其中创建和修改文件。

Any ideas on getting this to work?

You could provide an absolute path to the log file but you should make sure that the account that is configured to run your web application under IIS has write permissions to this folder so that it can create and modify files inside.

一曲爱恨情仇 2024-12-04 08:28:00

正如 Darin 提到的,您是否确保运行 Web 应用程序的用户帐户有权访问您的日志文件夹。我认为相对路径应该没问题?还要确保该文件夹存在吗?

As Darin mentioned, did you ensure that the user account that the web application is running under has access to your logs folder. I would think a relative path should be fine though? Also make sure the folder exists?

多情出卖 2024-12-04 08:28:00

如果您需要从代码中设置 logPath,而不是通过在配置文件中配置它,则可以这样做:

默认情况下,ELMAH 会读取您的 web.config 文件以找出要执行的 ErrorLog 实现创造。不过,您可以重写此机制来实现您自己的 IServiceProvider 实现,并使用 ServiceCenter.Current 属性注册返回实例的委托。

这样的 IServiceProvider 实现如下所示:

internal sealed class ElmahServiceProvider : IServiceProvider
{
    private readonly ErrorLog defaultErrorLog;

    private ElmahServiceProvider(ErrorLog defaultErrorLog)
    {
        Requires.IsNotNull(defaultErrorLog, "defaultErrorLog");
        this.defaultErrorLog = defaultErrorLog;
    }

    public object GetService(Type serviceType)
    {
        return serviceType == typeof(ErrorLog) ? this.defaultErrorLog : null;
    }
}

在应用程序启动期间,您可以执行以下操作:

// Option 1: using a virtual path
var logger = new XmlFileErrorLog(new Dictionary<string, string>
{
    { "logPath", "~/myCustomPath" }
});

// Option 2: using a fixed path
var logger = new XmlFileErrorLog("c:\\logFiles");

// Creating the provider and registering it in the ServiceCenter.
var provider = new ElmahServiceProvider(logger);
ServiceCenter.Current = c => provider;

这使您不必恢复到反射。

If you need to set the logPath from code instead of by configuring it in the config file, this is how to do it:

By default, ELMAH reads your web.config file to find out which ErrorLog implementation to create. You can however override this mechanism to implement your own IServiceProvider implementation and registering a delegate that returns an instance using the ServiceCenter.Current property.

Such IServiceProvider implementation looks like this:

internal sealed class ElmahServiceProvider : IServiceProvider
{
    private readonly ErrorLog defaultErrorLog;

    private ElmahServiceProvider(ErrorLog defaultErrorLog)
    {
        Requires.IsNotNull(defaultErrorLog, "defaultErrorLog");
        this.defaultErrorLog = defaultErrorLog;
    }

    public object GetService(Type serviceType)
    {
        return serviceType == typeof(ErrorLog) ? this.defaultErrorLog : null;
    }
}

And during application start, you can do the following:

// Option 1: using a virtual path
var logger = new XmlFileErrorLog(new Dictionary<string, string>
{
    { "logPath", "~/myCustomPath" }
});

// Option 2: using a fixed path
var logger = new XmlFileErrorLog("c:\\logFiles");

// Creating the provider and registering it in the ServiceCenter.
var provider = new ElmahServiceProvider(logger);
ServiceCenter.Current = c => provider;

This allows you prevents you from having to revert to reflection.

撕心裂肺的伤痛 2024-12-04 08:27:59

ELMAH logPath 只能在 Web.config 中配置,可以是虚拟路径(如果以“~/”开头)或绝对文件系统路径。 Web.config 中 ELMAH logPath 的默认配置是:

<elmah>
    <errorLog type="Elmah.XmlFileErrorLog, Elmah" logPath="~/App_Data/Elmah.Errors" />

但是有一个解决方法如何以编程方式设置 ELMAH logPath:

public class MvcApplication : HttpApplication
{
    private static string _elmahDirectory;
    private static readonly FieldInfo ElmahLogPathField = typeof(XmlFileErrorLog).GetField("_logPath", BindingFlags.NonPublic | BindingFlags.Instance);

    protected void Application_Start()
    {
        // Assign your path here
        _elmahDirectory = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments);
        ...
    }

    protected void Application_BeginRequest()
    {
        var xmlFileErrorLog = (XmlFileErrorLog)Elmah.ErrorLog.GetDefault(HttpContext.Current);
        ElmahLogPathField.SetValue(xmlFileErrorLog, _elmahDirectory);
    }
}

ELMAH logPath can be configured in Web.config only and could be either virtual path (if started with "~/") or absolute file system path. The default configuration of ELMAH logPath in Web.config is:

<elmah>
    <errorLog type="Elmah.XmlFileErrorLog, Elmah" logPath="~/App_Data/Elmah.Errors" />

But there is a workaround how to set ELMAH logPath programmatically:

public class MvcApplication : HttpApplication
{
    private static string _elmahDirectory;
    private static readonly FieldInfo ElmahLogPathField = typeof(XmlFileErrorLog).GetField("_logPath", BindingFlags.NonPublic | BindingFlags.Instance);

    protected void Application_Start()
    {
        // Assign your path here
        _elmahDirectory = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments);
        ...
    }

    protected void Application_BeginRequest()
    {
        var xmlFileErrorLog = (XmlFileErrorLog)Elmah.ErrorLog.GetDefault(HttpContext.Current);
        ElmahLogPathField.SetValue(xmlFileErrorLog, _elmahDirectory);
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文