jjchiw / gelf4net

GELF log4net Appender - graylog2
MIT License
63 stars 59 forks source link

Trying to do simple logging from a WinForms application #53

Closed dsiconnelli closed 6 years ago

dsiconnelli commented 6 years ago

I looked at the code of the SimpleConsoleApplicationHttpPackage console app. Pretty simple and it works with a local (in virtual box) Graylog2 VM (for now). However I tried the three lines of code which seem to matter and no message is sent.

class Program { protected static readonly ILog log = LogManager.GetLogger(typeof(Program)); static void Main(string[] args) { log4net.Config.XmlConfigurator.Configure(); Console.WriteLine("Write a sentence, q to quit"); var text = Console.ReadLine(); while(text != "q") { log.Debug(String.Format("Randomizer Sentence: {0}", text)); Console.WriteLine("Sent {0}", text); text = Console.ReadLine(); Console.WriteLine("Sent"); } } }

The configs are the same. My code is as follow.

private void btnError_Click(object sender, EventArgs e) { GLAppender.SendMessage(txtMessage.Text); }

class GLAppender
{
    protected static readonly ILog log = LogManager.GetLogger(typeof(Program));
    public static void SendMessage(string message)
    {
        log4net.Config.XmlConfigurator.Configure();
        log.Debug(message);
    } 
}

UPDATE: Now I have the simplest code I can, still nothing is sent :-(. It's almost a copy paste from the example project.

public partial class Form1 : Form { protected static readonly ILog log = LogManager.GetLogger(typeof(Program));

    public Form1()
    {
        InitializeComponent();
    }

    private void btnError_Click(object sender, EventArgs e)
    {
        log4net.Config.XmlConfigurator.Configure();
        log.Debug(txtMessage.Text);
    }

}
jjchiw commented 6 years ago

There is a Windows Form example app

Have you tried this example

https://github.com/jjchiw/gelf4net/tree/master/examples/RandomSentence

just change the App.config and check if it works.....

The only difference that I see between your code is that you configure log4net everytime the event btnError_Click(object sender, EventArgs e) is fired, try to configure log4net in the ctr

 public Form1()
    {
        InitializeComponent();
        log4net.Config.XmlConfigurator.Configure();
    }

    private void btnError_Click(object sender, EventArgs e)
    {
        log.Debug(txtMessage.Text);
    }

And if you can paste your App.config to see the configuration of log4net

dsiconnelli commented 6 years ago

Oh sorry. Thanks. What I have now (based on the example) is this. Starting the App or clicking buttons does nothing. I changed a bit the config and it's a copy paste now minus the Graylog2 Url.

   public partial class Form1 : Form
   {
    protected static readonly ILog log = LogManager.GetLogger(typeof(Form1));

    public Form1()
    {
        InitializeComponent();
        log4net.Config.XmlConfigurator.Configure();
    }

    private void Form1_Load(object sender, EventArgs e)
    {
        log.Debug("Starting the Application IA.SendTrace");
        LogSomething();
    }

    private void LogSomething()
    {
        log.Debug(String.Format("Application IA.SendTrace: {0}", txtMessage.Text));
    }

    private void btnMessage_Click(object sender, EventArgs e)
    {
        log.Debug(String.Format("Application IA.SendTrace: {0}", txtMessage.Text));
    }
dsiconnelli commented 6 years ago

Ok. I figured out what was the difference but I'm not shure why. Both apps are now working with my local Graylog2

In the consoles app, I have the nuget packages log4net 2.0.8 and Gelf4Net.HttpAppender 1.0.0.11

In the WinForms app I have log4net 2.0.8 and Gelf4Net 3.0.0.7.

What I had in the WinForms app were the same Nuget packages installed as the console app (log4net with Gelf4Net.HttpAppender 1.0.0.11) and that didn't work.

Not shure why as I thought that I only needed Gelf4Net.HttpAppender and that Gelf4Net had all the other appenders I don't need.

jjchiw commented 6 years ago

Not shure why as I thought that I only needed Gelf4Net.HttpAppender and that Gelf4Net had all the other appenders I don't need.

Technically that was the objective of creating the "standalone-packages" I'm not sure why it's not working in a WinForms App.... I'll create some samples with "standalone-packages" in a WinForm App....

Great that everything is working in local, now let's hope that when targeting to Azure everything works fine :)

I'll close this issue and create a new one that will be focused in create samples