سؤال

I want to get log4net and elmah working. I tried to put the code together in AppHost.cs:

public class AppHost : AppHostBase {
    //Tell ServiceStack the name and where to find your web services
    public AppHost() : base("Backbone.js TODO", typeof(TodoService).Assembly) { }

    public override void Configure(Funq.Container container) {
        //...
        //Log4Net 1.2.11
        log4net.Config.XmlConfigurator.Configure(new FileInfo("log4net.config"));
        container.Register(x => new Log4NetFactory(true));
        //Elmah 
        container.Register(x => new ElmahLogFactory(container.Resolve<Log4NetFactory>()));
        LogManager.LogFactory = container.Resolve<ElmahLogFactory>();
        container.Register<ILog>(x => LogManager.GetLogger(GetType())); 
        //...
    }

Log4net.config, which is currently just a console logger...

<?xml version="1.0" encoding="utf-8" ?>
    <log4net debug="true">
  <appender name="TraceAppender" type="log4net.Appender.TraceAppender">
    <layout type="log4net.Layout.PatternLayout">
      <conversionPattern value="%method %-5level - %message%newline"/>
    </layout>
  </appender>
  <root>
    <level value="DEBUG"/>
    <appender-ref ref="TraceAppender"/>
  </root>
</log4net>

Then wherever the logger is required:

ILog Logger {get; set;} //injected by ioc
//...
Logger.Info("Print something");

The Logger.Info("...."); line is called without error, but neither Log4Net nor Elmah is printing any output in my code above. If you have experience setting up log4net or elmah with ServiceStack, could you please point out What I have done wrong?


EDIT

Changed the FileInfo to full path, it worked as Stefan Egli suggested!

Or click log4net.config xml file in your project, in Properties box

"Copy to Output -> Copy if newer"

That will do, too.

Then, something like this comes up:

Auto-attach to process '[11308] w3wp.exe' on machine 'Desktop' succeeded.
//... printing info now ...
Backbone.Todos.AppHost: Info INFO  - TodoService OnGet 
//... but then after a coffee break ...
log4net: Hierarchy: Shutdown called on Hierarchy [log4net-default-repository]
// ... Oops, no longer printing any log ...

It seems like the web app went to shutdown itself after being idle for awhile. Then log4net fails to restart again when I click on the web app ... Any idea how to prevent this?


EDIT 2

Switched to NLog, happily ever after...

هل كانت مفيدة؟

المحلول

Elmah is for exception logging, so I would not expect to see anything in the elmah log based on what you posted so far. Log4net probably cannot find the configuration file: Try using a full path in order to see if this fixes the problem. If it does you need to figure out how you get the correct path dynamically.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top