This project has moved. For the latest updates, please go here.

Relative path to store log files

Oct 17, 2011 at 1:43 PM

I am using System.Diagnostics + Essential.Diagnostics for my logger in my ASP.NET MVC 3 app. Here is my web.config:
<system.diagnostics>
<trace autoflush="true" />
<sources>
<source name="requests" switchValue="All">
<listeners>
<remove name="default" />
<add name="rollingxml" />
</listeners>
</source>
</sources>
<sharedListeners>
<add name="rollingxml"
type="Essential.Diagnostics.RollingXmlTraceListener, Essential.Diagnostics"
initializeData="e:\myapi\v2\App_Data\Logfiles\{ApplicationName}-{LocalDateTime:yyyy-MM-dd}.svclog"
/>
</sharedListeners>
</system.diagnostics>

As you can see, the folder path to store the log files are hardcoded as e:\myapi\v2\App_Data\Logfiles\.

I want to put the log files into my app's app_data folder, but since the app is put in different locations in my dev PC, the staging server, and the main server, I need to put relative path in the initializeData. Something like .\App_Data\Logfiles\.

I have tried .\App_Data\Logfiles\ but it went to c:\windows\system32\inetsrv\App_Data\Logfiles\.

Is it possible to point to my app folder using relative path with Essential.Diagnostics?

Coordinator
Oct 21, 2011 at 10:18 PM

Endy,

Good to hear that you are making use of the project!

The 'current working folder' (i.e. ".") for web apps is under System32, like you describe, and there isn't any token or simliar at the moment.

One of the planned features is improved integration with ASP.NET adding tokens for various information from HttpContext (if available). I had more thought of things like the current page URL and the like, whereas you want HttpContext.Current.Server.MapPath(""), which is actually more of a function than a property.

I am thinking I could add something like the following:

   (planned) <add ... initializeData="{WebPhysicalPath}\App_Data\Logfiles\{ApplicationName}-{LocalDateTime:yyyy-MM-dd}.svclog" />

Generally, however you don't want to store data arbitrarily inside the web root because it could be a unexpected security issue and expose it to access via URL. Generally good practice would be to store it outside the web directory, although this doesn't apply to App_Data which is specially protected (and designed for storing raw data). Also, if you have a hosted service you simply may not have access outside of your web root, so have to use App_Data.

Given this, maybe App_Data is a better token, e.g.

   (planned) <add ... initializeData="{App_Data}\Logfiles\{ApplicationName}-{LocalDateTime:yyyy-MM-dd}.svclog" />

An alternative would be to have a mapPath="false|true" attribute on the listener that, if enabled, applies MapPath to the initializeData, so you would put in:

   (planned) <add ... initializeData="App_Data/Logfiles/{ApplicationName}-{LocalDateTime:yyyy-MM-dd}.svclog" mapPath="true" />


Which do you like/prefer?

Either add a Feature request to the Issue Tracker, and vote on it, or let me know which you would prefer and I will add to the Issue Tracker.

 

Sly

 

 

Oct 22, 2011 at 2:44 PM

On Oct 22, 2011, at 5:18 AM, "sgryphon" <notifications@codeplex.com> wrote:

I am thinking I could add something like the following:

(planned) <add ... initializeData="{WebPhysicalPath}\App_Data\Logfiles\{ApplicationName}-{LocalDateTime:yyyy-MM-dd}.svclog" />

Generally, however you don't want to store data arbitrarily inside the web root because it could be a unexpected security issue and expose it to access via URL. Generally good practice would be to store it outside the web directory, although this doesn't apply to App_Data which is specially protected (and designed for storing raw data). Also, if you have a hosted service you simply may not have access outside of your web root, so have to use App_Data.

Given this, maybe App_Data is a better token, e.g.

(planned) <add ... initializeData="{App_Data}\Logfiles\{ApplicationName}-{LocalDateTime:yyyy-MM-dd}.svclog" />

Since I want to store the log in app_data, I'm fine with all above. I prefer {App_Data} because it is more direct. If I need to access other folder, I can do {App_Data}\..\log for example.

An alternative would be to have a mapPath="false|true" attribute on the listener that, if enabled, applies MapPath to the initializeData, so you would put in:

(planned) <add ... initializeData="App_Data/Logfiles/{ApplicationName}-{LocalDateTime:yyyy-MM-dd}.svclog" mapPath="true" />

I prefer {App_Data}. Mappath is less intuitive for me.

Coordinator
Oct 28, 2011 at 1:51 AM

I've put a note in the issue "Add HttpContext items (if available) to TraceFormatter" ([workitem:11]), so vote for that if you want some of the ASP.NET context (including {App_Data}).

Feb 17, 2015 at 6:52 PM
Your comment about the Current Working Directory sounds believable, but then this baffles me. When I use System.Diagnostics.XmlWriterTraceListener with a relative path (e.g. "....\logs\trace.xml"), it honors it relative to my application. I've been relying on that for years locally with Cassini and now IIS Express (I use a full path in my deployed environments for other reasons).

But the moment I switch to Essential.Diagnostics.RollingXmlTraceListener, the path gets interpreted in some other manner that no longer works.

.NET's XmlWriterTraceListener seems to use Path.GetFullPath(fileName) to arrive at the full path from the path specified at initialization.