0

I need use log4net create a .txt and write log into .txt I did not konw what happen log4net not do anyting no create .txt and write log.

This is my log4net.config

<configuration>
    <configSections>   
        <sectionname="log4net"type="log4net.Config.Log4NetConfigurationSectionHandler, log4net"/>
    </configSections>

    <log4net>
        <root>
            <level value="ALL" />
            <appender-ref ref="MyAppender" />
            <appender-ref ref="RollingFileAppender" />
        </root>
        <appender name="MyAppender" type="log4net.Appender.ConsoleAppender">
            <layout type="log4net.Layout.PatternLayout">
                <conversionPattern value="%date %level %logger - %message%newline" />
            </layout>
        </appender>
        <appender name="MyFileAppender" type="log4net.Appender.FileAppender">
            <file value="application.log" />
            <appendToFile value="true" />
            <lockingModel type="log4net.Appender.FileAppender+MinimalLock" />
            <layout type="log4net.Layout.PatternLayout">
                <conversionPattern value="%date %level %logger - %message%newline" />
            </layout>
        </appender>
        <appender name="RollingFileAppender" type="log4net.Appender.RollingFileAppender">
            <file value="rolling.log" />
            <appendToFile value="true" />
            <rollingStyle value="Size" />
            <maxSizeRollBackups value="5" />
            <maximumFileSize value="10MB" />
            <staticLogFileName value="true" />
            <layout type="log4net.Layout.PatternLayout">
                <conversionPattern value="%date [%thread] %level %logger - %message%newline" />
            </layout>
        </appender>
    </log4net>

    <startup>
        <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5" />
    </startup>
</configuration>       

this is Program.cs use log.info() and log.error

private static readonly log4net.ILoglog = log4net.LogManager.GetLogge(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);


static void Main(string[] args)
{
    log.Info("log_info");
    log.Error("log_error");
}

this is assemblyinfo.cs

[assembly: log4net.Config.XmlConfigurator(ConfigFile = "log4net.config", Watch = true)]    
Filburt
  • 17,626
  • 12
  • 64
  • 115
edwardlim
  • 3
  • 3
  • Is your log4net config in the log4net.config file or in app.config? – toadflakz Aug 19 '16 at 08:12
  • There's a typo in your code sample: .GetLogge should be .GetLogge**r**. – Filburt Aug 19 '16 at 08:18
  • in my code is GetLogger – edwardlim Aug 19 '16 at 08:38
  • log4net config in the log4net.config file – edwardlim Aug 19 '16 at 08:39
  • 1
    Your log4net config file should just contain the `` elements. Apart from that, 0) is the file even being copied to the output 1) is the assembly attribute defined in the startup project and 2) are you making a [logging call at startup](http://logging.apache.org/log4net/release/manual/configuration.html#ConfigurationAttributes). Turning on [log4net internal debugging](http://stackoverflow.com/questions/756125/how-to-track-down-log4net-problems) will help as well. – stuartd Aug 19 '16 at 09:47
  • In my main I add BasicConfigurator.Configure(); it is work, my console will display the result. But nothing log file created. – edwardlim Aug 22 '16 at 00:21

1 Answers1

0

I found the problem, I have two .config one is App.config two is log4net.config if only write "log4net.config" ,it will not found the log4net.config.So need write the log4net.config path it will work.

            [assembly: log4net.Config.XmlConfigurator(ConfigFile =  @"C:\Users\username\Documents\Visual Studio 2015\Projects\Log\Log\log4net.config", Watch = true)]    
edwardlim
  • 3
  • 3