1

I am having one console app that having log4net(V-2.0.15) for logs. Configs are given below :

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
    <configSections>
        <section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler,Log4net"/>
    </configSections>
    <connectionStrings>
    </connectionStrings>
    <appSettings>
        <!--<add key="log4net.Internal.Debug" value="true"/>-->
    </appSettings>
<log4net>
    <appender name="RollingLogFileAppender" type="log4net.Appender.RollingFileAppender">
        <file value="C:\logs1\log.txt" />
        <encoding value="utf-8" />
        <appendToFile value="true" />
        <rollingStyle value="Date" />
        <!--<maxSizeRollBackups value="10" />
        <maximumFileSize value="100MB" />
        <staticLogFileName value="true" />-->
        <layout type="log4net.Layout.PatternLayout">
            <conversionPattern value="%-5p %d %5rms %-22.22c{1} %-18.18M - %m%n" />
        </layout>
    </appender>
    <root>
        <level value="All" />
        <appender-ref ref="RollingLogFileAppender" />
    </root>
</log4net>
<startup>
    <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.7.2" />
</startup>

C# Code is given below :

    private static readonly ILog Log = LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
    static void Main(string[] args)
    {
        try
        {
            ///Coding...
        }
        catch (Exception ex)
        {
            Log.Error("------- Error Message : " + ex.Message + "------- StackTrace : " + ((ex.StackTrace != null) ? ex.StackTrace.ToString() : ""));
        }
    }

Now I publish that console app to local folder and while creating 'Task Scheduler -> Basic Task' I pick exe file and created it. But it is not doing anything. What am I missing here???

TylerH
  • 20,799
  • 66
  • 75
  • 101
iDipa
  • 347
  • 2
  • 9
  • 20

1 Answers1

0

log4net.config

<log4net>
  <root>
    <level value="All" />
    <appender-ref ref="YourLog" />
  </root>

  <appender name="YourLog" type="log4net.Appender.RollingFileAppender">
    <file value="logs/yourlog.log" />
    <appendToFile value="true" />
    <rollingStyle value="Size" />
    <maxSizeRollBackups value="3" />
    <maximumFileSize value="5MB" />
    <staticLogFileName value="true" />
    <layout type="log4net.Layout.PatternLayout">
      <param name="ConversionPattern" value="%-5p|%d{ dd.MM.yyyy HH:mm:ss}|%m|%method|%stacktrace{5} (line %line)%newline" />
    </layout>
  </appender>
</log4net>

AssemblyInfo.cs

// some other stuffs
[assembly: log4net.Config.XmlConfigurator(ConfigFile = "log4net.config")]

code

    private static readonly ILog _log= LogManager.GetLogger("YourLog");

    private static void Main(string[] args)
    {

      try
      {

      }

      catch (Exception e)
      {
         _log.Error("Oh no! Something bad happened", e);
      }
     }

The name of the appender and the .config it does not matter. Only for best practices I would use log4net, since later you would need another config. App.config after build will be YOUR-PROJECT-NAME.config, this is more for projects configuration.

rick
  • 479
  • 5
  • 14
  • In my solution, app.config having that log4net config. what is log4net.config? and where it is located?? – iDipa Aug 02 '23 at 12:04
  • this is how Log4net will read your config. It should be in your project. – rick Aug 02 '23 at 12:05
  • After publishing, I am having this much files in publish folder. Not getting which to update - EPPlus.dll.deploy - EPPlus.Interfaces.dll.deploy - EPPlus.System.Drawing.dll.deploy - log4net.dll.deploy - Microsoft.IO.RecyclableMemoryStream.dll.deploy - TaskScheduler -- Applocation Manifest - TaskScheduler.exe.config.deploy - TaskScheduler.exe.deploy - TaskScheduler.exe.manifest – iDipa Aug 02 '23 at 12:27
  • Can you please provide me any link? to which I can follow steps and check in my solution? – iDipa Aug 02 '23 at 12:33
  • Ahh ok, i didnt know you lack in this. I will improve my answer. – rick Aug 02 '23 at 14:14