How do I write to a custom event source using .net core's ILoggerFactory? Is there a way to specify the...












1















I am trying to write to a custom event source in .netcore/c# but have not found a way to specify the target source of .net core's logger object. In this case, I want to write to "My Event Log" as opposed to the Application log. The code below writes successfully to the Application log, but I want to point it at the "My Event Log" event source.



if (!EventLog.SourceExists("My Event Log"))
{
EventLog.CreateEventSource("My Event Log", "My Program");
}

ILoggerFactory loggerFactory = new LoggerFactory()
.AddConsole()
.AddDebug()
.AddEventLog();

ILogger logger = loggerFactory.CreateLogger<Program>();
logger.LogInformation("DAILY LOAD starting...");









share|improve this question




















  • 1





    Isn't there an overload that takes EventLogSettings? There you can set the SourceName property.

    – Mike Zboray
    Jan 17 at 22:16











  • @MikeZboray - Using the overload was the solution, thanks! I expanded upon some other tripping points in getting the whole thing configured as well in my answer.

    – John Nettles
    Jan 18 at 21:28
















1















I am trying to write to a custom event source in .netcore/c# but have not found a way to specify the target source of .net core's logger object. In this case, I want to write to "My Event Log" as opposed to the Application log. The code below writes successfully to the Application log, but I want to point it at the "My Event Log" event source.



if (!EventLog.SourceExists("My Event Log"))
{
EventLog.CreateEventSource("My Event Log", "My Program");
}

ILoggerFactory loggerFactory = new LoggerFactory()
.AddConsole()
.AddDebug()
.AddEventLog();

ILogger logger = loggerFactory.CreateLogger<Program>();
logger.LogInformation("DAILY LOAD starting...");









share|improve this question




















  • 1





    Isn't there an overload that takes EventLogSettings? There you can set the SourceName property.

    – Mike Zboray
    Jan 17 at 22:16











  • @MikeZboray - Using the overload was the solution, thanks! I expanded upon some other tripping points in getting the whole thing configured as well in my answer.

    – John Nettles
    Jan 18 at 21:28














1












1








1








I am trying to write to a custom event source in .netcore/c# but have not found a way to specify the target source of .net core's logger object. In this case, I want to write to "My Event Log" as opposed to the Application log. The code below writes successfully to the Application log, but I want to point it at the "My Event Log" event source.



if (!EventLog.SourceExists("My Event Log"))
{
EventLog.CreateEventSource("My Event Log", "My Program");
}

ILoggerFactory loggerFactory = new LoggerFactory()
.AddConsole()
.AddDebug()
.AddEventLog();

ILogger logger = loggerFactory.CreateLogger<Program>();
logger.LogInformation("DAILY LOAD starting...");









share|improve this question
















I am trying to write to a custom event source in .netcore/c# but have not found a way to specify the target source of .net core's logger object. In this case, I want to write to "My Event Log" as opposed to the Application log. The code below writes successfully to the Application log, but I want to point it at the "My Event Log" event source.



if (!EventLog.SourceExists("My Event Log"))
{
EventLog.CreateEventSource("My Event Log", "My Program");
}

ILoggerFactory loggerFactory = new LoggerFactory()
.AddConsole()
.AddDebug()
.AddEventLog();

ILogger logger = loggerFactory.CreateLogger<Program>();
logger.LogInformation("DAILY LOAD starting...");






c# logging .net-core loggerfactory






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Jan 18 at 15:09









David Rogers

88411536




88411536










asked Jan 17 at 21:05









John NettlesJohn Nettles

63




63








  • 1





    Isn't there an overload that takes EventLogSettings? There you can set the SourceName property.

    – Mike Zboray
    Jan 17 at 22:16











  • @MikeZboray - Using the overload was the solution, thanks! I expanded upon some other tripping points in getting the whole thing configured as well in my answer.

    – John Nettles
    Jan 18 at 21:28














  • 1





    Isn't there an overload that takes EventLogSettings? There you can set the SourceName property.

    – Mike Zboray
    Jan 17 at 22:16











  • @MikeZboray - Using the overload was the solution, thanks! I expanded upon some other tripping points in getting the whole thing configured as well in my answer.

    – John Nettles
    Jan 18 at 21:28








1




1





Isn't there an overload that takes EventLogSettings? There you can set the SourceName property.

– Mike Zboray
Jan 17 at 22:16





Isn't there an overload that takes EventLogSettings? There you can set the SourceName property.

– Mike Zboray
Jan 17 at 22:16













@MikeZboray - Using the overload was the solution, thanks! I expanded upon some other tripping points in getting the whole thing configured as well in my answer.

– John Nettles
Jan 18 at 21:28





@MikeZboray - Using the overload was the solution, thanks! I expanded upon some other tripping points in getting the whole thing configured as well in my answer.

– John Nettles
Jan 18 at 21:28












1 Answer
1






active

oldest

votes


















0














Code and explanation below that got my .NETCore/C# console app writing to a "custom application log" in event viewer successfully WITHOUT Event ID Description errors (see ISSUE #1 FIX if you are having this issue).



//libraries needed..
//using Microsoft.Extensions.Logging;
//using Microsoft.Extensions.Logging.EventLog;

string _sourceName = "My Program"; //source program name
string _logName = "My Event Log"; //new event log or targeted event log name

//if custom event log does not exist, create it
if (!EventLog.SourceExists(_logName))
{
//event log creates new app log and associates program, "My Program", with new log, "My Event Log"
EventLog.CreateEventSource(_sourceName, _logName);
}

EventLogSettings myEventLogSettings = new EventLogSettings
{
SourceName = _sourceName,
LogName = _logName
};

ILoggerFactory loggerFactory = new LoggerFactory()
.AddConsole()
.AddDebug()
.AddEventLog(myEventLogSettings);

ILogger logger = loggerFactory.CreateLogger<Program>();
logger.LogInformation(1000, "DAILY LOAD starting...");


NOTE #1: if you are changing the targeted log of an existing source program... or in other words, if you are re-pointing a program that is ALREADY writing to an event log, this will require a reboot to register. If the log is newly created, it will write successfully. See this thread for more info: Windows Event Log - how to register an event source?



ISSUE #1: When you open event viewer to take a look at your custom application log's first event, you will see that your event is buried in an error message.



The description for Event ID 0 from source My Program cannot be found. Either the component that raises this event is not installed on your local computer or the installation is corrupted. You can install or repair the component on the local computer.
If the event originated on another computer, the display information had to be saved with the event.The following information was included with the event:
myProgram
DAILY LOAD starting...the message resource is present but the message is not found in the string/message table




ISSUE #1 FIX: the new application log is either referencing a non-existing Message Table file or the EventId being passed is NOT in the Message Table. The easiest way to resolve this issue is to have your log reference an existing Message Table file (for example, .NET Runtime) and pass it a documented EventId (I used 1000). This requires a registry change, steps below (article for reference and screenshots):





  1. Open registry

  2. Navigate to ComputerHKEY_LOCAL_MACHINESYSTEMCurrentControlSetServicesEventLog

  3. Click on the folder of the new event log. In this case, "My Event Log".


  4. Create new string in the folder.



    Value name = "EventMessageFile"

    Value data = "C:WindowsSystem32mscoree.dll"




IMPORTANT: the Value data path above is referencing the existing .NET Runtime Message table, you can reference a different message table if you would like. Because the .NET Runtime message table does NOT have an EventID equal to 0, I continued to get the same error. Once I changed the eventId being passed to an existing value in the .NET Runtime Message table (in this case, 1000), it worked as expected with NO error message!








share|improve this answer























    Your Answer






    StackExchange.ifUsing("editor", function () {
    StackExchange.using("externalEditor", function () {
    StackExchange.using("snippets", function () {
    StackExchange.snippets.init();
    });
    });
    }, "code-snippets");

    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "1"
    };
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function() {
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled) {
    StackExchange.using("snippets", function() {
    createEditor();
    });
    }
    else {
    createEditor();
    }
    });

    function createEditor() {
    StackExchange.prepareEditor({
    heartbeatType: 'answer',
    autoActivateHeartbeat: false,
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });














    draft saved

    draft discarded


















    StackExchange.ready(
    function () {
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54244285%2fhow-do-i-write-to-a-custom-event-source-using-net-cores-iloggerfactory-is-the%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    0














    Code and explanation below that got my .NETCore/C# console app writing to a "custom application log" in event viewer successfully WITHOUT Event ID Description errors (see ISSUE #1 FIX if you are having this issue).



    //libraries needed..
    //using Microsoft.Extensions.Logging;
    //using Microsoft.Extensions.Logging.EventLog;

    string _sourceName = "My Program"; //source program name
    string _logName = "My Event Log"; //new event log or targeted event log name

    //if custom event log does not exist, create it
    if (!EventLog.SourceExists(_logName))
    {
    //event log creates new app log and associates program, "My Program", with new log, "My Event Log"
    EventLog.CreateEventSource(_sourceName, _logName);
    }

    EventLogSettings myEventLogSettings = new EventLogSettings
    {
    SourceName = _sourceName,
    LogName = _logName
    };

    ILoggerFactory loggerFactory = new LoggerFactory()
    .AddConsole()
    .AddDebug()
    .AddEventLog(myEventLogSettings);

    ILogger logger = loggerFactory.CreateLogger<Program>();
    logger.LogInformation(1000, "DAILY LOAD starting...");


    NOTE #1: if you are changing the targeted log of an existing source program... or in other words, if you are re-pointing a program that is ALREADY writing to an event log, this will require a reboot to register. If the log is newly created, it will write successfully. See this thread for more info: Windows Event Log - how to register an event source?



    ISSUE #1: When you open event viewer to take a look at your custom application log's first event, you will see that your event is buried in an error message.



    The description for Event ID 0 from source My Program cannot be found. Either the component that raises this event is not installed on your local computer or the installation is corrupted. You can install or repair the component on the local computer.
    If the event originated on another computer, the display information had to be saved with the event.The following information was included with the event:
    myProgram
    DAILY LOAD starting...the message resource is present but the message is not found in the string/message table




    ISSUE #1 FIX: the new application log is either referencing a non-existing Message Table file or the EventId being passed is NOT in the Message Table. The easiest way to resolve this issue is to have your log reference an existing Message Table file (for example, .NET Runtime) and pass it a documented EventId (I used 1000). This requires a registry change, steps below (article for reference and screenshots):





    1. Open registry

    2. Navigate to ComputerHKEY_LOCAL_MACHINESYSTEMCurrentControlSetServicesEventLog

    3. Click on the folder of the new event log. In this case, "My Event Log".


    4. Create new string in the folder.



      Value name = "EventMessageFile"

      Value data = "C:WindowsSystem32mscoree.dll"




    IMPORTANT: the Value data path above is referencing the existing .NET Runtime Message table, you can reference a different message table if you would like. Because the .NET Runtime message table does NOT have an EventID equal to 0, I continued to get the same error. Once I changed the eventId being passed to an existing value in the .NET Runtime Message table (in this case, 1000), it worked as expected with NO error message!








    share|improve this answer




























      0














      Code and explanation below that got my .NETCore/C# console app writing to a "custom application log" in event viewer successfully WITHOUT Event ID Description errors (see ISSUE #1 FIX if you are having this issue).



      //libraries needed..
      //using Microsoft.Extensions.Logging;
      //using Microsoft.Extensions.Logging.EventLog;

      string _sourceName = "My Program"; //source program name
      string _logName = "My Event Log"; //new event log or targeted event log name

      //if custom event log does not exist, create it
      if (!EventLog.SourceExists(_logName))
      {
      //event log creates new app log and associates program, "My Program", with new log, "My Event Log"
      EventLog.CreateEventSource(_sourceName, _logName);
      }

      EventLogSettings myEventLogSettings = new EventLogSettings
      {
      SourceName = _sourceName,
      LogName = _logName
      };

      ILoggerFactory loggerFactory = new LoggerFactory()
      .AddConsole()
      .AddDebug()
      .AddEventLog(myEventLogSettings);

      ILogger logger = loggerFactory.CreateLogger<Program>();
      logger.LogInformation(1000, "DAILY LOAD starting...");


      NOTE #1: if you are changing the targeted log of an existing source program... or in other words, if you are re-pointing a program that is ALREADY writing to an event log, this will require a reboot to register. If the log is newly created, it will write successfully. See this thread for more info: Windows Event Log - how to register an event source?



      ISSUE #1: When you open event viewer to take a look at your custom application log's first event, you will see that your event is buried in an error message.



      The description for Event ID 0 from source My Program cannot be found. Either the component that raises this event is not installed on your local computer or the installation is corrupted. You can install or repair the component on the local computer.
      If the event originated on another computer, the display information had to be saved with the event.The following information was included with the event:
      myProgram
      DAILY LOAD starting...the message resource is present but the message is not found in the string/message table




      ISSUE #1 FIX: the new application log is either referencing a non-existing Message Table file or the EventId being passed is NOT in the Message Table. The easiest way to resolve this issue is to have your log reference an existing Message Table file (for example, .NET Runtime) and pass it a documented EventId (I used 1000). This requires a registry change, steps below (article for reference and screenshots):





      1. Open registry

      2. Navigate to ComputerHKEY_LOCAL_MACHINESYSTEMCurrentControlSetServicesEventLog

      3. Click on the folder of the new event log. In this case, "My Event Log".


      4. Create new string in the folder.



        Value name = "EventMessageFile"

        Value data = "C:WindowsSystem32mscoree.dll"




      IMPORTANT: the Value data path above is referencing the existing .NET Runtime Message table, you can reference a different message table if you would like. Because the .NET Runtime message table does NOT have an EventID equal to 0, I continued to get the same error. Once I changed the eventId being passed to an existing value in the .NET Runtime Message table (in this case, 1000), it worked as expected with NO error message!








      share|improve this answer


























        0












        0








        0







        Code and explanation below that got my .NETCore/C# console app writing to a "custom application log" in event viewer successfully WITHOUT Event ID Description errors (see ISSUE #1 FIX if you are having this issue).



        //libraries needed..
        //using Microsoft.Extensions.Logging;
        //using Microsoft.Extensions.Logging.EventLog;

        string _sourceName = "My Program"; //source program name
        string _logName = "My Event Log"; //new event log or targeted event log name

        //if custom event log does not exist, create it
        if (!EventLog.SourceExists(_logName))
        {
        //event log creates new app log and associates program, "My Program", with new log, "My Event Log"
        EventLog.CreateEventSource(_sourceName, _logName);
        }

        EventLogSettings myEventLogSettings = new EventLogSettings
        {
        SourceName = _sourceName,
        LogName = _logName
        };

        ILoggerFactory loggerFactory = new LoggerFactory()
        .AddConsole()
        .AddDebug()
        .AddEventLog(myEventLogSettings);

        ILogger logger = loggerFactory.CreateLogger<Program>();
        logger.LogInformation(1000, "DAILY LOAD starting...");


        NOTE #1: if you are changing the targeted log of an existing source program... or in other words, if you are re-pointing a program that is ALREADY writing to an event log, this will require a reboot to register. If the log is newly created, it will write successfully. See this thread for more info: Windows Event Log - how to register an event source?



        ISSUE #1: When you open event viewer to take a look at your custom application log's first event, you will see that your event is buried in an error message.



        The description for Event ID 0 from source My Program cannot be found. Either the component that raises this event is not installed on your local computer or the installation is corrupted. You can install or repair the component on the local computer.
        If the event originated on another computer, the display information had to be saved with the event.The following information was included with the event:
        myProgram
        DAILY LOAD starting...the message resource is present but the message is not found in the string/message table




        ISSUE #1 FIX: the new application log is either referencing a non-existing Message Table file or the EventId being passed is NOT in the Message Table. The easiest way to resolve this issue is to have your log reference an existing Message Table file (for example, .NET Runtime) and pass it a documented EventId (I used 1000). This requires a registry change, steps below (article for reference and screenshots):





        1. Open registry

        2. Navigate to ComputerHKEY_LOCAL_MACHINESYSTEMCurrentControlSetServicesEventLog

        3. Click on the folder of the new event log. In this case, "My Event Log".


        4. Create new string in the folder.



          Value name = "EventMessageFile"

          Value data = "C:WindowsSystem32mscoree.dll"




        IMPORTANT: the Value data path above is referencing the existing .NET Runtime Message table, you can reference a different message table if you would like. Because the .NET Runtime message table does NOT have an EventID equal to 0, I continued to get the same error. Once I changed the eventId being passed to an existing value in the .NET Runtime Message table (in this case, 1000), it worked as expected with NO error message!








        share|improve this answer













        Code and explanation below that got my .NETCore/C# console app writing to a "custom application log" in event viewer successfully WITHOUT Event ID Description errors (see ISSUE #1 FIX if you are having this issue).



        //libraries needed..
        //using Microsoft.Extensions.Logging;
        //using Microsoft.Extensions.Logging.EventLog;

        string _sourceName = "My Program"; //source program name
        string _logName = "My Event Log"; //new event log or targeted event log name

        //if custom event log does not exist, create it
        if (!EventLog.SourceExists(_logName))
        {
        //event log creates new app log and associates program, "My Program", with new log, "My Event Log"
        EventLog.CreateEventSource(_sourceName, _logName);
        }

        EventLogSettings myEventLogSettings = new EventLogSettings
        {
        SourceName = _sourceName,
        LogName = _logName
        };

        ILoggerFactory loggerFactory = new LoggerFactory()
        .AddConsole()
        .AddDebug()
        .AddEventLog(myEventLogSettings);

        ILogger logger = loggerFactory.CreateLogger<Program>();
        logger.LogInformation(1000, "DAILY LOAD starting...");


        NOTE #1: if you are changing the targeted log of an existing source program... or in other words, if you are re-pointing a program that is ALREADY writing to an event log, this will require a reboot to register. If the log is newly created, it will write successfully. See this thread for more info: Windows Event Log - how to register an event source?



        ISSUE #1: When you open event viewer to take a look at your custom application log's first event, you will see that your event is buried in an error message.



        The description for Event ID 0 from source My Program cannot be found. Either the component that raises this event is not installed on your local computer or the installation is corrupted. You can install or repair the component on the local computer.
        If the event originated on another computer, the display information had to be saved with the event.The following information was included with the event:
        myProgram
        DAILY LOAD starting...the message resource is present but the message is not found in the string/message table




        ISSUE #1 FIX: the new application log is either referencing a non-existing Message Table file or the EventId being passed is NOT in the Message Table. The easiest way to resolve this issue is to have your log reference an existing Message Table file (for example, .NET Runtime) and pass it a documented EventId (I used 1000). This requires a registry change, steps below (article for reference and screenshots):





        1. Open registry

        2. Navigate to ComputerHKEY_LOCAL_MACHINESYSTEMCurrentControlSetServicesEventLog

        3. Click on the folder of the new event log. In this case, "My Event Log".


        4. Create new string in the folder.



          Value name = "EventMessageFile"

          Value data = "C:WindowsSystem32mscoree.dll"




        IMPORTANT: the Value data path above is referencing the existing .NET Runtime Message table, you can reference a different message table if you would like. Because the .NET Runtime message table does NOT have an EventID equal to 0, I continued to get the same error. Once I changed the eventId being passed to an existing value in the .NET Runtime Message table (in this case, 1000), it worked as expected with NO error message!









        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Jan 18 at 21:26









        John NettlesJohn Nettles

        63




        63






























            draft saved

            draft discarded




















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54244285%2fhow-do-i-write-to-a-custom-event-source-using-net-cores-iloggerfactory-is-the%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            Homophylophilia

            Updating UILabel text programmatically using a function

            Cloud Functions - OpenCV Videocapture Read method fails for larger files from cloud storage