For MVC application custom listener does not create a log file when initializeData="CustomWeblog.txt" parameter is used, but initializeData="d:\CustomWeblog.txt" triggers file creation. What is the reason of such behaviour? Console application generates files for all types of listeners.
Custom class:
public class CustomTextWriterTraceListener : TextWriterTraceListener
{
public CustomTextWriterTraceListener(string fileName) : base(fileName)
}
Web.config (mvc application, web.config)
<trace autoflush="true" />
<sources>
<source name="Trace">
<listeners>
<add name="TextWriterListner"
type="System.Diagnostics.TextWriterTraceListener, WebTracing" initializeData="Weblog.txt"/>
<!-- the file is created -->
<add name="CustomTextWriterListner"
type="WebTracing.CustomTextWriterTraceListener, WebTracing" initializeData="CustomWeblog.txt"/>
<!-- the file is not created in MVC application ?! -->
<add name="CustomTextWriterListnerAbsolutePath"
type="WebTracing.CustomTextWriterTraceListener, WebTracing" initializeData="d:\CustomWeblog.txt"/>
<!-- the file is created -->
</listeners>
</source>
</sources>
Cutom listener does not create a log file.
Caller:
TraceSource obj = new TraceSource("Trace", SourceLevels.All);
obj.TraceEvent(TraceEventType.Critical,0,"This is a critical message");
I have tried to add some extra configuration: from this blog and this one. But there is no success. Should I provide a absolute path? Is there any workaround by creating a separate assembly for custom listener?
Ok, finally, I have switched investigation to the way listener paths are generated. What I have noticed on debugging is that source listeners list contain different paths.
The different values as caused by the reason that custom listened swallowed UnauthorisedAccessException exception so that the application continues working without informing us about permissions issues.
The following link to the TextWriterTraceListener source code helped me to figure out the path. The following code:
Actual storage path depends on Project > Properties > Web > Server: IIS Express:
All the time I was debudding the MVC application (as an administrator: vs run as administrator) log files were correctly generated in that folder. When I am running VS without administrator permissions custom listeners do not create files at all.
As it was mentined above, I executed the source code listener and found that
catch(UnauthorisedAccessException) { break; }
is triggered onnew StreamWriter(...)
constructor call.As another workaround you can declare the whole path in
initializeData="d:\CustomWeblog.txt"
attribute. But keep in mind that you have to have the proper permissions.I was trying to create my own rolling text writer trace listener when I was encountering the same issue you described. Long story short, after all the running around here is what I came up with.
UPDATE: Here is the utility class I wrote for resolving paths.
So this utility class allows me to resolve relative paths and also customize formats. For example, if you looked at the code you would have seen that application name
ApplicationName
variable does not exist in this pathI am able to configure that in the startup of the application along with any other variables I want to add like so