나는 최근에 ASP.net vnext의 AssemblyNeutral
속성을 발견했습니다.ASp.net의 AssemblyNeutral 속성 vnext
특정해야 할 경우 로깅 리포지토리에서 해당 특성 사용을 찾을 수 있습니다. https://github.com/aspnet/logging
다른 많은 저장소가 있지만 이것은 단순한 저장소입니다.
우리가 AssemblyNeutral
속성을 사용하는 경우 많은 블로그 당 같이 그런 다음 인터페이스 또는 유형은 어셈블리와 연결되지 않으므로 같은 계약을 원하는 다른 프레임 워크에서 AssemblyNeutral
속성과 동일한 인터페이스를 정의 할 수 있습니다.
그래서 예제를 만들면 ASP.net vnext 클래스 라이브러리 프로젝트에서 내 자신의 작은 로거 라이브러리를 만들었지 만 유형 캐스트 오류가 발생합니다.
namespace MyLogger
{
#if ASPNET50 || ASPNETCORE50
[Microsoft.Framework.Runtime.AssemblyNeutral]
#endif
public interface ILogger
{
void Write(LogLevel logLevel, int eventId, object state, Exception exception, Func<object, Exception, string> formatter);
bool IsEnabled(LogLevel logLevel);
IDisposable BeginScope(object state);
}
#if ASPNET50 || ASPNETCORE50
[Microsoft.Framework.Runtime.AssemblyNeutral]
#endif
public interface ILoggerFactory
{
ILogger Create(string name);
void AddProvider(ILoggerProvider provider);
}
#if ASPNET50 || ASPNETCORE50
[Microsoft.Framework.Runtime.AssemblyNeutral]
#endif
public interface ILoggerProvider
{
ILogger Create(string name);
}
#if ASPNET50 || ASPNETCORE50
[Microsoft.Framework.Runtime.AssemblyNeutral]
#endif
public enum LogLevel
{
Verbose = 1,
Information = 2,
Warning = 3,
Error = 4,
Critical = 5,
}
public class MyLogger : ILogger
{
public IDisposable BeginScope(object state)
{
return null;
}
public bool IsEnabled(LogLevel logLevel)
{
return true;
}
public void Write(LogLevel logLevel, int eventId, object state, Exception exception, Func<object, Exception, string> formatter)
{
System.IO.File.AppendAllText("C:\\Testlog.txt", "This is test log" + System.Environment.NewLine);
}
}
public class MyLoggerProvider : ILoggerProvider
{
private readonly Func<string, LogLevel, bool> _filter;
public MyLoggerProvider(Func<string, LogLevel, bool> filter)
{
_filter = filter;
}
public ILogger Create(string name)
{
return new MyLogger();
}
}
public static class MyLoggerExtentions
{
public static ILoggerFactory AddMyLogger(this ILoggerFactory factory, Func<string, LogLevel, bool> filter)
{
factory.AddProvider(new MyLoggerProvider(filter));
return factory;
}
}
}
이제 SampleApp (로깅 저장소 SampleApp)에서 사용하려고합니다. 전송 오류가 발생하는 것을 볼 수 있습니다. 왜 그렇게?
예. 당신 말이 맞아 아마도 내가 당신의 블로그를 읽었지만 IDE를 사용하지 않고 어떻게 예제를 컴파일 할 수 있습니까? 액션에서 그 속성을보고 싶습니다. – dotnetstep