Git Product home page Git Product logo

Comments (6)

JocelyneElKhoury7 avatar JocelyneElKhoury7 commented on July 23, 2024 1

@huorswords yes it was solved with changing the initialization thank you

from microsoft.extensions.logging.log4net.aspnetcore.

huorswords avatar huorswords commented on July 23, 2024

Hello @JocelyneElKhoury7 ,

Excuse me to answer you so late, but I'm very busy at my job and I don't have so much time on the last weeks.

Could you please provide a sample or some code in order to reproduce your issue?

It is difficult to me to reproduce your scenario from scratch.

Thank you.

from microsoft.extensions.logging.log4net.aspnetcore.

JocelyneElKhoury7 avatar JocelyneElKhoury7 commented on July 23, 2024

Hello @huorswords,

I have fixed it and now it is logging, but i got another issue now, the same line is being logged several times, it turned out that because i have to set logger.AddLog4Net() in the constructor of every class, if i don't set it the logging won't work, and when i set it the same line is logged several times(the number of times i have initiated the logger.AddLog4Net() ) can you help me plz? below is my code, note that i am using .net core windows service 2.2

    class Program
       {
          static void Main(string[] args)
          {
             ILoggerFactory loggerFactory = new LoggerFactory();
             ILogger<Program> logger;
             logger = loggerFactory.CreateLogger<Program>();
             loggerFactory.AddLog4Net();
             logger.LogInformation("Program.Main");
          try
           {
            var builder = new ConfigurationBuilder()
                                .SetBasePath(Path.GetDirectoryName(Assembly.GetEntryAssembly().Location))
                              .AddJsonFile("appsettings.json");

            var configuration = builder.Build();
            StaticVariables.SqlConnStr = configuration["ReportingDbSqlCnx"];

            var serviceProvider = new ServiceCollection()
                .AddLogging()
                .AddMemoryCache()
                .AddTransient<IGeneralDA, GeneralDA>()
                .AddTransient<IGeneral, General>()
                .BuildServiceProvider();

            serviceQuartz.StartJobs().GetAwaiter().GetResult();

            using (var service1 = new ReportingService(serviceQ))
            {
                ServiceBase.Run(service1);
            }

        }
        catch (Exception ex)
        {
            logger.LogError(ex.ToString());
        }
       
    }
}

the logs in the program class is logged once but in the other classes it is logged several times

     public class Class1 : IClass1
     {
        private readonly IGeneral _general;
        private readonly ILogger<QuartzScheduler> _logger;

        public Class1(IGeneral general, ILoggerFactory loggerFactory)
        {
        _general = general;
        loggerFactory.AddLog4Net();
        _logger = loggerFactory.CreateLogger<Class1>();
    }

    public async Task Start()
    {
        try
        {
            _logger.LogInformation("Class1.Start.Started");
            
            _logger.LogInformation("Class1.Start.Ended");
        }
        catch (Exception se)
        {
            _logger.LogError(se.ToString());
        }
    }

}

i tried this also

public Class1(IGeneral general, ILogger<Class1> logger)
    {
        _general = general;
        _logger = logger;
    }

but in this case the logger did not write in the file

from microsoft.extensions.logging.log4net.aspnetcore.

JocelyneElKhoury7 avatar JocelyneElKhoury7 commented on July 23, 2024

ok i fixed it for whoever faces it, i fixed it in this line

    var serviceProvider = new ServiceCollection()
                .AddLogging(configure => configure.AddLog4Net())

and i used this in all my classes

  public Class1(IGeneral general, ILogger<Class1> logger)
   {
        _general = general;
        _logger = logger;
   }

from microsoft.extensions.logging.log4net.aspnetcore.

taadis avatar taadis commented on July 23, 2024

@JocelyneElKhoury7 I met the same problem. and you can commit a pull request...

from microsoft.extensions.logging.log4net.aspnetcore.

huorswords avatar huorswords commented on July 23, 2024

@JocelyneElKhoury7 thank you very much by updating the issue.

As far as I understand, your problem is solved and the root cause was an incorrect initialization, isn't it?

I'm happy to know that you already resolved it. 👍

from microsoft.extensions.logging.log4net.aspnetcore.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.