Descriptions for logging that exist in a class.

Asked 1 years ago, Updated 1 years ago, 111 views

We are developing it in .NET Framework 4.0.
Logging descriptions are scattered throughout the class.
Also, the log output description in the class makes the class unusable for general purposes.
As the readability has decreased, I would like to ask you a question to solve this problem you.

The current situation is as follows.

private class x
private subexec()
-- Start processing log output description
-- Action 1
  Class X. Method A
  ---- Class X. Method A handles XX data
   Class X. Method A Successful
-- Process 2
  Class Y. Method A
  ---- Class Y. Method A handles XX data
   Class Y. Method A Successful
-- End of process log output description
End sub
end class

A class for logging output is provided and the method for that class is
when the log output is made. I'm calling by passing arguments, but I'm not sure if the description straddles several classes.
Anyway, it becomes complicated.
The same mechanism is needed not only for logging purposes, but also for working with form windows that represent progress.

http://www.itmedia.co.jp/im/articles/0410/20/news086.html
I used this site to look into AOP and DI, but I couldn't come up with a specific solution.

If you know any specific solution, please let me know.

visual-studio vb.net delegate

2022-09-30 17:50

2 Answers

For .NET, how about PostSharp?
Free for commercial use (Can I use PostSharp Express for commercial products? on This page
The free version has functional limitations, but I think log output is sufficient.

I'm sorry for the C# example, but I'll use it like this:

 MyLogging
public class ClassX
{
    public int MethodA(...)
    {
        ...
    }
}

The MyLogging attribute inherits a class in the PostSharp library and creates it yourself.

public sold class MyLoggingAttribute:OnMethodBoundaryAspect
{
     public override void OnEntry (MethodExecutionArgsargs)
     {
         // be called before processing the target method
         // Arguments and instances of the target class are included in args.
     }
     public override void OnExit (MethodExecutionArgs args)
     {
         // Called after processing the target method (after return)
         // args is the same as OnEntry's, but there is also a return value.
     }
}

If you attach this attribute to a class, each time a method for that class is called, OnEntry and OnExit are called, so you can write log output processing or something like that.
If you attribute a method instead of a class, only that method can be logged out.

"However, I think some ingenuity is necessary to produce output like ""processing XX data."""
Make sure to return the number of cases with the return value...

PostSharp is available immediately after searching online in Visual Studio's Extensions and Updates.

As a supplement, there are many ways to implement the AOP framework, and PostSharp uses the method of "rewrite IL during build".
So, depending on the quantity, the build will be delayed.
However, compared to using proxy classes, there are no restrictions such as "must inherit a class" and performance at runtime is advantageous.


2022-09-30 17:50

We introduce NLog to help you with your design examples or to consider replacing them with proprietary implementations.

using NLog;

public class MyClass
{
  private static Logger logger = LogManager.GetCurrentClassLogger();

  public void MyMethod1()
  {
    logger.Trace("Sample trace message");
    logger.Debug("Sample debug message");
    logger.Info("Sample informational message");
    logger.Warn("Sample warning message");
    logger.Error("Sample error message");
    logger.Fatal("Sample fatal error message");

    // alternately you can call the Log() method 
    // and pass log level as the parameter.
    logger.Log(LogLevel.Info, "Sample informational message");
  }
}

Logger is generated for static members of the class and utilized for each class.Other examples are well organized in Wiki, so please read them.
The log output processing to be written in the code is only at the log message and level (Fatal, Error, ...), and its output destination (Target) and format is controlled by NLog.Config.The design is clear and I like it.


2022-09-30 17:50

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.