Skip to content

NeuralLog/csharp-sdk

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

5 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

NeuralLog C# SDK

The C# SDK for NeuralLog provides a client library for interacting with the NeuralLog server from C# applications. It offers a familiar logging API and adapters for popular .NET logging frameworks.

Requirements

  • .NET 9.0 or later

Installation

# Install via NuGet Package Manager
Install-Package NeuralLog.SDK

# Or using .NET CLI
dotnet add package NeuralLog.SDK

Basic Usage

using NeuralLog.SDK;
using System.Collections.Generic;

// Configure the SDK
var config = new NeuralLogConfig
{
    ServerUrl = "http://localhost:3030",
    Namespace = "default",
    AsyncEnabled = true,
    BatchSize = 100,
    BatchIntervalMs = 5000
};
NeuralLog.Configure(config);

// Get a logger
var logger = NeuralLog.GetLogger("my-application");

// Log a simple message
logger.Info("Hello, world!");

// Log with structured data
var data = new Dictionary<string, object>
{
    ["user"] = "john.doe",
    ["action"] = "login",
    ["ip"] = "192.168.1.1"
};
logger.Info("User logged in", data);

// Log an error with exception
try
{
    // Some code that might throw an exception
    throw new System.Exception("Something went wrong");
}
catch (System.Exception ex)
{
    logger.Error("Failed to process request", ex);
}

Configuration Options

The C# SDK supports various configuration options:

var config = new NeuralLogConfig
{
    // Required settings
    ServerUrl = "https://logs.example.com",
    Namespace = "production",

    // Optional settings
    ApiKey = "your-api-key",
    BatchSize = 100,
    BatchIntervalMs = 5000,
    MaxRetries = 3,
    RetryBackoffMs = 1000,
    AsyncEnabled = true,
    DebugEnabled = false,

    // HTTP client settings
    Timeout = TimeSpan.FromSeconds(30),
    MaxConnections = 10,

    // Custom HTTP headers
    Headers = new Dictionary<string, string>
    {
        ["X-Custom-Header"] = "value"
    }
};

NeuralLog.Configure(config);

Advanced Features

Context Data

// Set global context data for all loggers
NeuralLog.SetGlobalContext(new Dictionary<string, object>
{
    ["application"] = "my-application",
    ["environment"] = "production",
    ["version"] = "1.0.0"
});

// Set logger-specific context data
logger.SetContext(new Dictionary<string, object>
{
    ["service"] = "user-service",
    ["instance"] = "user-service-1"
});

Batching

The SDK supports batching of log messages to improve performance:

var config = new NeuralLogConfig
{
    ServerUrl = "http://localhost:3030",
    Namespace = "default",
    AsyncEnabled = true,
    BatchSize = 100,
    BatchIntervalMs = 5000
};
NeuralLog.Configure(config);

With batching enabled, log messages are queued and sent in batches when:

  • The batch size is reached
  • The batch interval elapses
  • The Flush method is called

Flushing

To ensure all pending log messages are sent, you can flush the logger:

// Flush a specific logger
await logger.Flush();

// Flush all loggers
await NeuralLog.FlushAll();

Framework Integration

The NeuralLog C# SDK can be integrated with popular .NET logging frameworks. Framework-specific adapters will be added in future releases.

Building from Source

# Clone the repository
git clone https://github.com/NeuralLog/csharp.git
cd csharp

# Build the solution
dotnet build

# Run the tests
dotnet test

Documentation

Detailed documentation is available in the docs directory:

For integration guides and tutorials, visit the NeuralLog Documentation Site.

Contributing

Contributions are welcome! Please read our Contributing Guide for details on our code of conduct and the process for submitting pull requests.

Related NeuralLog Components

License

MIT

About

C# SDK for NeuralLog

Resources

Contributing

Stars

Watchers

Forks

Packages

 
 
 

Contributors