Tag: 日志

如何在log4net消息中添加类别前缀?

我想为现有日志消息中的所有消息添加类别前缀。 但是,将此前缀逐个添加到所有现有日志记录消息中非常繁琐。 有没有办法可以在类级别添加一个属性,那么这个类中的所有消息都会记录到某个类别? 而不是现在的方式如下, Log.Info(“[Ref] Level 1 Starts …”); 我真的想要这样或类似的方式来定义log4net.ILog。 [LoggingCategory(“Ref”)] public class MyClass { public void MyMethod() { Log.Info(“Level 1 Starts …”); } }

在c#中记录按键时输入特殊字符时显示的双字符

我有一个应用程序记录用户按下的任何内容,但是当我按下特殊字符时,例如´ with a , ´´a ,to ´´a ; 同样的事情,当我想得到à ,然后我得到“a ,所以所有特殊字符输入两次然后常规字符输入后。 我一直在搜索,真的找不到任何东西。 但我注意到问题出在ToAscii方法中,没有正确输入字符。 public string GetString(IntPtr lParam, int vCode) { try { bool shift = Keys.Shift == Control.ModifierKeys || Console.CapsLock; string value = “”; KeyboardHookStruct MyKeyboardHookStruct = (KeyboardHookStruct)Marshal.PtrToStructure( lParam, typeof(KeyboardHookStruct)); byte[] keyState = new byte[256]; byte[] inBuffer = new byte[2]; DllClass.GetKeyboardState(keyState); var ascii= DllClass.ToAscii( MyKeyboardHookStruct.vkCode, […]

Serilog的logger包装器的实现和使用

这个问题与史蒂文的答案有关 – 这里 。 他提出了一个非常好的记录器包装器。 我将在下面粘贴他的代码: public interface ILogger { void Log(LogEntry entry); } public static class LoggerExtensions { public static void Log(this ILogger logger, string message) { logger.Log(new LogEntry(LoggingEventType.Information, message, null)); } public static void Log(this ILogger logger, Exception exception) { logger.Log(new LogEntry(LoggingEventType.Error, exception.Message, exception)); } // More methods here. } 所以,我的问题是创建代理Serilog的实现的正确方法是什么 ? 注意:这个问题与关于log4net的这个问题有关,但现在特定于Serilog。

如何确定调用方法和类名?

我目前正在使用内置的TraceListener开发应用程序日志库。 这个库将在许多项目中使用,并且应该提供一个简单的界面,我只需要关心将什么内容写入日志文件,而不是如何。 通过使用reflection命名空间,我可以找出当前称为日志函数的应用程序(检索执行程序集名称),但我还想要调用日志记录函数的函数和类的名称。 假设我有: public static void LogInfo(string vLogText) { Trace.WriteLine( MethodInfo.GetCurrentMethod().Name + this.GetType().ToString() + vLogText); } 当我从另一个项目调用时(类:TestClass,方法:TestMethod) Tracer.LogInfo(“log this!”) 我希望在日志中看到: TestClass, TestMethod, log this! 但相反,我得到了 TracerClass, LogInfo, log this! 如何获取父方法和类名?