Elmah logPath outside wwwroot

浪子不回头ぞ 提交于 2019-12-13 14:26:37

问题


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.


回答1:


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.




回答2:


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);
    }
}



回答3:


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?




回答4:


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.



来源:https://stackoverflow.com/questions/6873614/elmah-logpath-outside-wwwroot

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!