Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Nuget package not working: Basic reproduceable example #100

Open
danammeansbear opened this issue Jul 8, 2024 · 0 comments
Open

Nuget package not working: Basic reproduceable example #100

danammeansbear opened this issue Jul 8, 2024 · 0 comments

Comments

@danammeansbear
Copy link

danammeansbear commented Jul 8, 2024

Hello all, I just tried to create a c# console program using the nuget package latest version and it is not working.

Console program
`
using System;
using log4net;
using log4net.Config;
using System.IO;
using System.Reflection;
using System.Net.Http;
using System.Net.Security;
using System.Security.Cryptography.X509Certificates;
using System.Threading.Tasks;
using log4stash;

[assembly: XmlConfigurator(ConfigFile = "log4net.config", Watch = true)]

namespace ELKTest
{
internal class Program
{
private static readonly ILog logger = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);

    static async Task Main(string[] args)
    {
        // Enable log4net internal debugging
        log4net.Util.LogLog.InternalDebugging = true;
        log4net.Config.XmlConfigurator.Configure();

        var logger = LogManager.GetLogger("elk_test");

        // Ignore SSL certificate errors (not recommended for production)
        //var handler = new HttpClientHandler();
        //handler.ServerCertificateCustomValidationCallback = (HttpRequestMessage, X509Certificate2, X509Chain, SslPolicyErrors) => true;
        //var client = new HttpClient(handler);

        //var request = new HttpRequestMessage(HttpMethod.Post, "server.com:9200/elk_test/_doc");
        //request.Headers.Add("Authorization", "Basic password");
        //var content = new StringContent("some message");
        //request.Content = content;

        //var response = await client.SendAsync(request);
        //response.EnsureSuccessStatusCode();
        //Console.WriteLine(await response.Content.ReadAsStringAsync());

//the above is test code used to make sure I can send messages to elk. Had to modify the code produced by postman because of certificate errors.
try
{
// Ensure log4net is configured
var logRepository = LogManager.GetRepository(Assembly.GetEntryAssembly());

            // Reflectively modify the HttpClientHandler of the ElasticSearchAppender
            var log4stashAppender = (ElasticSearchAppender)LogManager.GetRepository().GetAppenders()[0];
            var clientField = typeof(ElasticSearchAppender).GetField("_client", BindingFlags.NonPublic | BindingFlags.Instance);
            if (clientField != null)
            {
                var httpClient = (HttpClient)clientField.GetValue(log4stashAppender);
                var customHandler = new HttpClientHandler
                {
                    ServerCertificateCustomValidationCallback = (HttpRequestMessage, X509Certificate2, X509Chain, SslPolicyErrors) => true
                };
                var customClient = new HttpClient(customHandler);
                clientField.SetValue(log4stashAppender, customClient);
            }

            log4stashAppender.ActivateOptions();

            // Log messages
            logger.Debug("This is a DEBUG message.");
            logger.Info("This is an INFO message.");
            logger.Warn("This is a WARN message.");
            logger.Error("This is an ERROR message.");
            logger.Error("kaboom!", new ApplicationException("An error occurred"));
            Console.WriteLine("Log messages have been sent to Elasticsearch....maybe....");
        }
        catch (Exception ex)
        {
            Console.WriteLine("Error occurred: " + ex.Message);
        }
    }
}

}

`

here is my App.config
`

  <appender-ref ref="ElasticSearchAppender" />
  
</root>

<appender name="console" type="log4net.Appender.ConsoleAppender">
  <layout type="log4net.Layout.PatternLayout">
    <conversionPattern value="%date %level %logger - %message%newline" />
  </layout>
</appender>

<appender name="ElasticSearchAppender" type="log4stash.ElasticSearchAppender, log4stash">
  <!-- DEV -->
  <Server>server.com</Server>
  <!-- PRD 
  <Server>server.com</Server>
  -->
  <Port>9200</Port>
	<Ssl>false</Ssl>
	<AllowSelfSignedServerCert>true</AllowSelfSignedServerCert>
  <!--<IndexName>exs_log4net_%{+yyyy-MM-dd}</IndexName>-->
  <!--<IndexName>exs_log4net_%{+yyyy-MM}</IndexName>-->
  <IndexName>elk_test</IndexName>
  <IndexType>LogEvent</IndexType>
  <BulkSize>1</BulkSize>
  <BulkIdleTimeout>10000</BulkIdleTimeout>
  <IndexAsync>false</IndexAsync>
  <!-- optional: elasticsearch timeout for the request, default = 10000 -->
  <ElasticSearchTimeout>10000</ElasticSearchTimeout>
  <AuthenticationMethod>
    <!--For basic authentication purposes-->
    <Basic>
      <Username>user</Username>
      <Password>password</Password>
    </Basic>
  </AuthenticationMethod>
  <ElasticFilters>
    <Add>
      <Key>Environment</Key>
      <Value>DEV</Value>
    </Add>
    <Remove>
      <Key>log4net:Identity</Key>
    </Remove>
    <!-- example of using filter with default parameters -->
    <kv>
      <SourceKey>Message</SourceKey>
      <ValueSplit>|</ValueSplit>
      <FieldSplit> ,</FieldSplit>
    </kv>
  </ElasticFilters>
</appender>

`

here is the errors/output I am getting.

log4net: log4net assembly [log4net, Version=2.0.9.0, Culture=neutral, PublicKeyToken=669e0ddf0bb1aa2a]. Loaded from [C:\CodeRepo\Dev\ELKTest\ELKTest\bin\Debug\net5.0\log4net.dll]. (.NET Runtime [5.0.17] on Microsoft Windows NT 10.0.19045.0) log4net: defaultRepositoryType [log4net.Repository.Hierarchy.Hierarchy] log4net: Creating repository for assembly [ELKTest, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null] log4net: Assembly [ELKTest, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null] Loaded From [C:\CodeRepo\Dev\ELKTest\ELKTest\bin\Debug\net5.0\ELKTest.dll] log4net: Assembly [ELKTest, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null] does not have a RepositoryAttribute specified. log4net: Assembly [ELKTest, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null] using repository [log4net-default-repository] and repository type [log4net.Repository.Hierarchy.Hierarchy] log4net: Creating repository [log4net-default-repository] using type [log4net.Repository.Hierarchy.Hierarchy] log4net: configuring repository [log4net-default-repository] using file [C:\CodeRepo\Dev\ELKTest\ELKTest\bin\Debug\net5.0\log4net.config] watching for file updates log4net: configuring repository [log4net-default-repository] using file [C:\CodeRepo\Dev\ELKTest\ELKTest\bin\Debug\net5.0\log4net.config] log4net: configuring repository [log4net-default-repository] using stream log4net: loading XML configuration log4net: Configuring Repository [log4net-default-repository] log4net: Configuration update mode [Merge]. log4net: Logger [root] Level string is [ALL]. log4net: Logger [root] level set to [name="ALL",value=-2147483648]. log4net: Loading Appender [console] type: [log4net.Appender.ConsoleAppender] log4net: Converter [message] Option [] Format [min=-1,max=2147483647,leftAlign=False] log4net: Converter [newline] Option [] Format [min=-1,max=2147483647,leftAlign=False] log4net: Setting Property [ConversionPattern] to String value [%date %level %logger - %message%newline] log4net: Converter [date] Option [] Format [min=-1,max=2147483647,leftAlign=False] log4net: Converter [literal] Option [ ] Format [min=-1,max=2147483647,leftAlign=False] log4net: Converter [level] Option [] Format [min=-1,max=2147483647,leftAlign=False] log4net: Converter [literal] Option [ ] Format [min=-1,max=2147483647,leftAlign=False] log4net: Converter [logger] Option [] Format [min=-1,max=2147483647,leftAlign=False] log4net: Converter [literal] Option [ - ] Format [min=-1,max=2147483647,leftAlign=False] log4net: Converter [message] Option [] Format [min=-1,max=2147483647,leftAlign=False] log4net: Converter [newline] Option [] Format [min=-1,max=2147483647,leftAlign=False] log4net: Setting Property [Layout] to object [log4net.Layout.PatternLayout] log4net: Created Appender [console] log4net: Adding appender named [console] to logger [root]. log4net: Loading Appender [ElasticSearchAppender] type: [log4stash.ElasticSearchAppender, log4stash] log4net: Setting Property [Server] to String value [server] log4net: Setting Property [Port] to Int32 value [9200] log4net: Setting Property [Ssl] to Boolean value [False] log4net: Setting Property [AllowSelfSignedServerCert] to Boolean value [True] log4net: Setting Property [IndexName] to String value [elk_test] log4net: Setting Property [IndexType] to String value [LogEvent] log4net: Setting Property [BulkSize] to Int32 value [1] log4net: Setting Property [BulkIdleTimeout] to Int32 value [10000] log4net: Setting Property [IndexAsync] to Boolean value [False] log4net: Setting Property [ElasticSearchTimeout] to Int32 value [10000] log4net:ERROR Object type not specified. Cannot create object of type [log4stash.Authentication.AuthenticationMethodChooser]. Missing Value or Type. log4net:ERROR Failed to create object to set param: AuthenticationMethod log4net: Setting Property [Key] to String value [Environment] log4net: Setting Property [Value] to String value [DEV] log4net: Setting Collection Property [AddAdd] to object [log4stash.Filters.AddValueFilter] log4net: Setting Property [Key] to String value [log4net:Identity] log4net: Setting Collection Property [AddRemove] to object [log4stash.Filters.RemoveKeyFilter] log4net: Setting Property [SourceKey] to String value [Message] log4net: Setting Property [ValueSplit] to String value [|] log4net: Setting Property [FieldSplit] to String value [ ,] log4net: Setting Collection Property [AddKv] to object [log4stash.Filters.KvFilter] log4net: Setting Property [ElasticFilters] to object [log4stash.ElasticAppenderFilters] log4net: Created Appender [ElasticSearchAppender] log4net: Adding appender named [ElasticSearchAppender] to logger [root]. log4net: Hierarchy Threshold [] log4net: configuring repository [log4net-default-repository] using .config file section log4net: Application config file is [C:\CodeRepo\Dev\ELKTest\ELKTest\bin\Debug\net5.0\ELKTest.dll.config] log4net: Configuring Repository [log4net-default-repository] log4net: Configuration update mode [Merge]. log4net: Logger [root] Level string is [ALL]. log4net: Logger [root] level set to [name="ALL",value=-2147483648]. log4net: Loading Appender [console] type: [log4net.Appender.ConsoleAppender] log4net: Converter [message] Option [] Format [min=-1,max=2147483647,leftAlign=False] log4net: Converter [newline] Option [] Format [min=-1,max=2147483647,leftAlign=False] log4net: Setting Property [ConversionPattern] to String value [%date %level %logger - %message%newline] log4net: Converter [date] Option [] Format [min=-1,max=2147483647,leftAlign=False] log4net: Converter [literal] Option [ ] Format [min=-1,max=2147483647,leftAlign=False] log4net: Converter [level] Option [] Format [min=-1,max=2147483647,leftAlign=False] log4net: Converter [literal] Option [ ] Format [min=-1,max=2147483647,leftAlign=False] log4net: Converter [logger] Option [] Format [min=-1,max=2147483647,leftAlign=False] log4net: Converter [literal] Option [ - ] Format [min=-1,max=2147483647,leftAlign=False] log4net: Converter [message] Option [] Format [min=-1,max=2147483647,leftAlign=False] log4net: Converter [newline] Option [] Format [min=-1,max=2147483647,leftAlign=False] log4net: Setting Property [Layout] to object [log4net.Layout.PatternLayout] log4net: Created Appender [console] log4net: Adding appender named [console] to logger [root]. log4net: Loading Appender [ElasticSearchAppender] type: [log4stash.ElasticSearchAppender, log4stash] log4net: Setting Property [Server] to String value [server.com] log4net: Setting Property [Port] to Int32 value [9200] log4net: Setting Property [Ssl] to Boolean value [False] log4net: Setting Property [AllowSelfSignedServerCert] to Boolean value [True] log4net: Setting Property [IndexName] to String value [elk_test] log4net: Setting Property [IndexType] to String value [LogEvent] log4net: Setting Property [BulkSize] to Int32 value [1] log4net: Setting Property [BulkIdleTimeout] to Int32 value [10000] log4net: Setting Property [IndexAsync] to Boolean value [False] log4net: Setting Property [ElasticSearchTimeout] to Int32 value [10000] log4net:ERROR Object type not specified. Cannot create object of type [log4stash.Authentication.AuthenticationMethodChooser]. Missing Value or Type. log4net:ERROR Failed to create object to set param: AuthenticationMethod log4net: Setting Property [Key] to String value [Environment] log4net: Setting Property [Value] to String value [DEV] log4net: Setting Collection Property [AddAdd] to object [log4stash.Filters.AddValueFilter] log4net: Setting Property [Key] to String value [log4net:Identity] log4net: Setting Collection Property [AddRemove] to object [log4stash.Filters.RemoveKeyFilter] log4net: Setting Property [SourceKey] to String value [Message] log4net: Setting Property [ValueSplit] to String value [|] log4net: Setting Property [FieldSplit] to String value [ ,] log4net: Setting Collection Property [AddKv] to object [log4stash.Filters.KvFilter] log4net: Setting Property [ElasticFilters] to object [log4stash.ElasticAppenderFilters] log4net: Created Appender [ElasticSearchAppender] log4net: Adding appender named [ElasticSearchAppender] to logger [root]. log4net: Hierarchy Threshold [] Error occurred: Unable to cast object of type 'log4net.Appender.ConsoleAppender' to type 'log4stash.ElasticSearchAppender'. log4net: Shutdown called on Hierarchy [log4net-default-repository]

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant