Skip to content

www778878net/Log78

Repository files navigation

Log78

English | 简体中文

License Test Status QQ Group

Feedback QQ Group (Click to join): 323397913

1. Log78 Class Documentation

Overview

Log78 is a powerful and easy-to-use logging class that supports various types of log output including console, file, and server-side output. It uses the singleton pattern to ensure there's only one global instance, making it incredibly convenient to use throughout your application without explicit setup.

Installation

Install via NuGet Package Manager:

dotnet add package Log78

Quick Start

Log78 offers two ways to log messages: a simple method for quick logging and a more detailed method using LogEntry objects. Here's how to get started with the simple method:

using www778878net.log;

// Get the Log78 instance - no setup required!
var log = Log78.Instance;

// Log a simple message
log.INFO("Hello, Log78!");

// Log with a summary and custom level
log.WARN("This is a warning", "Warning Summary", 60);

For more detailed logging, you can use the LogEntry object:

var logEntry = new LogEntry();
logEntry.Basic.Message = "Detailed log message";
logEntry.Basic.Summary = "Log Summary";
log.INFO(logEntry);

Both methods are ready to use out of the box with default console and file logging.

Advanced Configuration (Optional)

If you need custom logging behavior, you can use the setup method:

// Create custom logger instances if needed
var serverLogger = new ServerLog78();
var fileLogger = new FileLog78("custom_logfile");
var consoleLogger = new ConsoleLog78();

// Setup custom loggers
log.setup(serverLogger, fileLogger, consoleLogger);

Properties

  • debugKind: A HashSet of log debugging keywords used to control which types of logs are recorded.
  • LevelFile, LevelConsole, LevelApi: Respectively represent the threshold levels for file logs, console logs, and API logs.
  • DebugEntry: Used to set more fine-grained debugging conditions.

Suggested Log Levels

  • DEBUG (10): Detailed debug information, typically used only in development environments
  • INFO (50): General information, can be used to track normal application operations
  • WARN (50): Warning information, indicating potential issues but not affecting main functionality
  • ERROR (70): Errors and serious problems that require immediate attention

Example: Adjusting Log Levels

using www778878net.log;
var log = Log78.Instance;

// Adjust console log level to 0 to print all logs (for debugging)
log.LevelConsole = 0;
// Adjust file log level to 60 to only record more severe warnings and errors
log.LevelFile = 60;

// Using different levels to record logs
var logEntry = new LogEntry();
logEntry.Basic.Message = "Debug information";
log.DEBUG(logEntry); // Will only output to console

logEntry.Basic.Message = "General information";
log.INFO(logEntry); // Will output to console, not recorded in file

logEntry.Basic.Message = "Warning";
log.WARN(logEntry); // Will be recorded in both console and file

logEntry.Basic.Message = "Error";
log.ERROR(logEntry); // Will be recorded in console, file, and API

Methods

  • DEBUG, INFO, WARN, ERROR: Record logs of different levels.
  • ERROR(Exception, LogEntry): Records exception error logs.

Using the LogEntry Class

The LogEntry class provides structured information for detailed logging:

var logEntry = new LogEntry();
logEntry.Basic.Summary = "User login successful";
logEntry.Basic.LogLevelNumber = 50;
logEntry.Basic.LogLevel = "INFO";
logEntry.Basic.Message = "User johndoe successfully logged into the system";
logEntry.Basic.ServiceName = "AuthService";
logEntry.Basic.UserId = "user123";
logEntry.Basic.UserName = "johndoe";

logEntry.Event.EventCategory = "authentication";
logEntry.Event.EventAction = "login";
logEntry.Event.EventOutcome = "success";

logEntry.Http.HttpRequestMethod = "POST";
logEntry.Http.HttpRequestBodyContent = "{\"username\":\"johndoe\",\"password\":\"*****\"}";
logEntry.Http.HttpResponseStatusCode = 200;
logEntry.Http.UrlOriginal = "https://api.example.com/login";

// Add custom properties
logEntry.AddProperty("customField", "customValue");

log.INFO(logEntry);

Other

For more detailed information, please refer to the project's GitHub repository or the API documentation.

About

Log78 is a simple NLog wrapper library designed for .NET 6.0 projects. It provides convenient logging functionality that can be used without complex configuration. It supports console, file, and server logging, and allows customization of log levels.

Resources

License

Stars

Watchers

Forks

Packages

 
 
 

Contributors