Trying to use Base MVC/Page and Model in the same view - asp.net-mvc-5

I have a base class that I'm trying to use in a View. I understand now that #model is really an implementation of 'WebViewPage'. So, I believe there is probably a better way to accomplish what I want, maybe with an Action Filter or my BaseController object.
I was trying to do something like this:
public abstract class AuthenticatedViewPageBase : WebViewPage
{
private Login _user;
protected override void InitializePage()
{
_user = Session["User"] as Login;
}
public bool HasPermission(Permissions permission)
{
return HasPermission(new List<Permissions> { permission });
}
public bool HasPermission(List<Permissions> permissions)
{
if (_user == null)
_user = Session["User"] as Login;
return _user != null && permissions.Any(thisPerm => _user.Permissions.Any(p => p.PermissionId == (int)thisPerm));
}
}
And use it in a List View like this:
#using PublicationSystem.Model.Enums
#inherits PublicationSystem.Helpers.AuthenticatedViewPageBase
#model IEnumerable<PublicationSystem.Model.Profile>
#{
ViewBag.Title = "Index";
Layout = "~/Views/Shared/_LayoutSmBanner.cshtml";
}
#if (HasPermission(new List<Permissions>
{
Permissions.userCreate
}))
{
<p>
#Html.ActionLink("Create New", "Create");
</p>
}
....
But of course, I cannot use #inherits and #model together. (The Profile class is a simple model.)
What would be a good way to get abilities of function like HasPermission in MVC?

I defined two similar classes:
public abstract class AuthenticatedViewPageBase : WebViewPage
{
//....
}
public abstract class AuthenticatedViewPageBase<TModel> : WebViewPage<TModel>
{
//....
}
Then set \Views\web.config like this:
<system.web.webPages.razor>
<host factoryType="System.Web.Mvc.MvcWebRazorHostFactory, System.Web.Mvc, Version=5.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" />
<pages pageBaseType="PublicationSystem.Helpers.AuthenticatedViewPageBase">
<namespaces>
<add namespace="System.Web.Mvc" />
<add namespace="System.Web.Mvc.Ajax" />
<add namespace="System.Web.Mvc.Html" />
<add namespace="System.Web.Routing" />
<add namespace="System.Web.Optimization" />
<add namespace="PublicationSystem" />
</namespaces>
</pages>
</system.web.webPages.razor>
Now my pages can use #model as normally and get the custom methods from my abstract version of WebViewPage.
#model IEnumerable<PublicationSystem.Model.Profile>

Related

No assembly found containing a Startup or [AssemblyName].Startup class

I've tried resolving this from answers in other and similar posts, but no luck.
I'm Using MVC 5, framework 4.8 latest VS2017.
Thanks
My Config is: (including other attempts)
<configuration>
<appSettings>
<!--<add key="owin:AutomaticAppStartup" value="false" />-->
<add key="owin:HandleAllRequests" value="true"/>
<!--<add key="owin:AppStartup" value="Api.xxx" />-->
</appSettings>
</configuration>
Startup class is:
[assembly: OwinStartupAttribute(typeof(Api.xxx.Startup))]
namespace Api.xxx
{
public class Startup
{
public void Configuration(IAppBuilder app)
{
// Allow all origins
app.UseCors(Microsoft.Owin.Cors.CorsOptions.AllowAll);
….
}
}
}
and Api is:
namespace Api.xxx
{
[Route("values")]
public class ValuesController : ApiController
{
private static readonly Random _random = new Random();
public IEnumerable<string> Get()
{
var random = new Random();
return new[]
{
_random.Next(0, 10).ToString(),
_random.Next(0, 10).ToString()
};
}
}
}
I think you need to change
[assembly: OwinStartupAttribute(typeof(Api.xxx.Startup))]
to
[assembly: OwinStartup(typeof(Api.xxx.Startup))]
Reference: https://learn.microsoft.com/en-us/aspnet/aspnet/overview/owin-and-katana/owin-startup-class-detection

Custom PatternLayoutConverter with log4net.Ext.Json?

I have the following log4net configuration:
<log4net>
<appender name="Console" type="log4net.Appender.ConsoleAppender">
<layout type='log4net.Layout.SerializedLayout, log4net.Ext.Json'>
<renderer type='log4net.ObjectRenderer.JsonDotNetRenderer, log4net.Ext.Json.Net'>
<DateFormatHandling value="IsoDateFormat" />
<NullValueHandling value="Ignore" />
</renderer>
<converter>
<name value="preparedMessage" />
<type value="JsonLogs.CustomLayoutConverter" />
</converter>
<default />
<remove value='message' />
<remove value='ndc' />
<member value='message:messageObject' />
<member value='details:preparedMessage' />
</layout>
</appender>
<appender name="Console2" type="log4net.Appender.ConsoleAppender">
<layout type="log4net.Layout.PatternLayout">
<converter>
<name value="preparedMessage" />
<type value="JsonLogs.CustomLayoutConverter" />
</converter>
<conversionPattern value="%level %thread %logger - %preparedMessage%newline" />
</layout>
</appender>
<root>
<level value="DEBUG" />
<appender-ref ref="Console" />
<appender-ref ref="Console2" />
</root>
</log4net>
with the following implementation of my custom PatternLayoutConverter:
namespace JsonLogs
{
using System.IO;
using log4net.Core;
using log4net.Layout.Pattern;
public class CustomLayoutConverter : PatternLayoutConverter
{
#region Methods
protected override void Convert(TextWriter writer, LoggingEvent loggingEvent)
{
if (loggingEvent.MessageObject is string stringMessage)
{
writer.Write(new { message = stringMessage });
}
else
{
writer.Write(loggingEvent.RenderedMessage);
}
}
#endregion
}
}
For some reason, the converter works perfectly fine with the Console2 appender(which is not JSON driven) but it doesn't work with the Console appender whose output is JSON.
Example of the output:
Console -> {"date":"2018-12-09T12:25:28.0529041+03:00","level":"INFO","appname":"JsonLogs.exe","logger":"JsonLogs.Program","thread":"1","message":"Test","details":"preparedMessage"}
Console2 -> INFO 1 JsonLogs.Program - { message = Test }
My goal is to have details always in JSON that's why I introduced my own converter to catch primitive values and wrap them in a custom object.
Is my configuration wrong? Or I'm missing something? Could you help me, please, to figure this out?
Thank you
The issue seems to be a bug of log4net.Ext.Json. I'm going to report it on their GitLab.
So far, I ended up with my custom log4net layout which looks like this
public class CustomLayout : PatternLayout
{
#region Public Methods and Operators
public override void Format(TextWriter writer, LoggingEvent loggingEvent)
{
var message = loggingEvent.MessageObject.GetType().IsPrimitive || loggingEvent.MessageObject is string || loggingEvent.MessageObject is decimal || loggingEvent.MessageObject is BigInteger
? new { message = loggingEvent.MessageObject }
: loggingEvent.MessageObject;
writer.WriteLine(JsonConvert.SerializeObject(new
{
timestamp = loggingEvent.TimeStampUtc,
threadId = loggingEvent.ThreadName,
details = message,
logger = loggingEvent.LoggerName,
level = loggingEvent.Level.DisplayName,
user = loggingEvent.UserName
}));
}
#endregion
}
it meets my needs and does exactly what I want.
The exact place of this problem is AddMember Method and its implementation. Here is SerializedLayout source code for that:
public virtual void AddMember(string value)
{
var arrangement = log4net.Util.TypeConverters.ArrangementConverter.GetArrangement(value, new ConverterInfo[0]);
m_arrangement.AddArrangement(arrangement);
}
As you can see the second parameter of GetArrangment is empty array of ConverterInfo, Though there must be our custom attached ones (by AddConverter method or by xml).
As the solution you can implement your own subclass that will derive from SerializedLayout with overridden AddMember like this:
public override void AddMember(string value)
{
var customConverter = new ConverterInfo("lookup", typeof(CustomPatternConverter));
var arrangement = log4net.Util.TypeConverters.ArrangementConverter.GetArrangement(value, new ConverterInfo[] { customConverter });
m_arrangement.AddArrangement(arrangement);
}
Hope it helps as it did with my case!

Unity Resolve<Interface1<Interface2>> issues

is it possible to resolve objects which use generics in unity only using interfaces.
class
public interface ItestObject<T>{ T Create();}
public class testObject<T> : ItestObject<T> where T: class, ICMSBasicTextData, new()
{
public testObject(){}
public T Create()
{
return new T();
}
}
xml config
<alias alias="testObject_I" type="JMJoinery.ItestObject`1, JMJoinery" />
<alias alias="testObject_C" type="JMJoinery.testObject`1[[JMJoinery.CMS.Data.CMSBasicTextData, JMJoinery]], JMJoinery" />
<alias alias="CMSBasicTextData_I" type="JMJoinery.CMS.Data.ICMSBasicTextData, JMJoinery" />
<alias alias="CMSBasicTextData_C" type="JMJoinery.CMS.Data.CMSBasicTextData, JMJoinery" />
<register name="CMSBasicTextData_R" type="CMSBasicTextData_I" mapTo="CMSBasicTextData_C" />
<register name="test" type="testObject_I" mapTo="testObject_C" />
when i try to resolve:
var o2 = JMJoinery.Global.Container.Resolve<ItestObject<ICMSBasicTextData>>();
this error occurs:
"Unable to cast object of type 'JMJoinery.testObject1[JMJoinery.CMS.Data.CMSBasicTextData]' to type 'JMJoinery.ItestObject1[JMJoinery.CMS.Data.ICMSBasicTextData]'."
Using
Container.Resolve<ItestObject<CMSBasicTextData>>();
works, but is it possible to resolve using the interface only
Container.Resolve<ItestObject<ICMSBasicTextData>>();
instead?
Interface and Implementation
public interface ILookupDataReader<T>
{
string LookupTypeID { get; }
}
public class CountryDataReader: ILookupDataReader<ICountry>
{
string ILookupDataReader<ICountry>.LookupTypeID
{
get { return Enum.GetName(typeof(LookupTypeOptions), LookupTypeOptions.Country); }
}
}
Registration
<alias alias="ILookupDataReader" type="Co.Application.Shared.Core.BusinessObject.ILookupDataReader`1,Co.Shared.Impl"/>
<alias alias="ICountry" type="Co.Application.Shared.Core.BusinessObject.Lookup.ICountry, Co.Shared.Impl"/>
<register type="ILookupDataReader[ICountry]" mapTo="Co.Application.Shared.Core.BusinessObject.Lookup.CountryDataReader,Co.Shared.Impl">
<lifetime type="singleton"/>
</register>
Usage
[Dependency]
public ILookupDataReader<ICountry> CountryLookup { get; set; }

log4net filter - how to write AND filter to ignore log messages

I am struggling to write an AND conditional filter in log4net. Had it been nLog, I could have written it this way:
<logger name="*" minlevel="Info" xsi:type="NLogLoggerRule" writeTo="FooLogger" >
<filters>
<when condition="equals('${event-context:item=UserID}', 'TESTUSER')
and equals('${event-context:item=URL}','/foo/foobar.aspx')"
action="Ignore" />
</filters>
</logger>
I am not sure how to write the same filter in log4net. I have been so far successful, in writing a single condition:
<appender>
....
<filter type="log4net.Filter.PropertyFilter">
<key value="URL" />
<stringToMatch value="/foo/foobar.aspx" />
<acceptOnMatch value="false" />
</filter>
</appender>
How can I write AND conditions using log4net filters? Please help.
A custom filter supporting AND conditions. This class exposes Filter property so existing log4net filters can be used here and also one can have nested AND conditions if required.
public class AndFilter : FilterSkeleton
{
private bool acceptOnMatch;
private readonly IList<IFilter> filters = new List<IFilter>();
public override FilterDecision Decide(LoggingEvent loggingEvent)
{
if (loggingEvent == null)
throw new ArgumentNullException("loggingEvent");
foreach(IFilter filter in filters)
{
if (filter.Decide(loggingEvent) != FilterDecision.Accept)
return FilterDecision.Neutral; // one of the filter has failed
}
// All conditions are true
if(acceptOnMatch)
return FilterDecision.Accept;
else
return FilterDecision.Deny;
}
public IFilter Filter
{
set { filters.Add(value); }
}
public bool AcceptOnMatch
{
get { return acceptOnMatch;}
set { acceptOnMatch = value;}
}
}
Config:
<filter type="Namespace.AndFilter, Assembly">
<filter type="log4net.Filter.PropertyFilter">
<key value="URL" />
<stringToMatch value="/foo/foobar.aspx" />
</filter>
<filter type="log4net.Filter.PropertyFilter">
<key value="UserID" />
<stringToMatch value="TESTUSER" />
</filter>
<acceptOnMatch value="false" />
</filter>
You can create custom filter for your business needs:
public class UserRequestFilter : FilterSkeleton
{
public override FilterDecision Decide(LoggingEvent loggingEvent)
{
if (loggingEvent == null)
throw new ArgumentNullException("loggingEvent");
string userId = (string)loggingEvent.Properties["UserId"];
string url = (string)loggingEvent.Properties["Url"];
if (String.IsNullOrEmpty(UserId) || String.IsNullOrEmpty(Url))
return FilterDecision.Neutral;
if (UserId.Equals(userId) && Url.Equals(url, StringComparison.CurrentCultureIgnoreCase))
return AcceptOnMatch ? FilterDecision.Accept : FilterDecision.Deny;
return FilterDecision.Neutral;
}
public bool AcceptOnMatch { get; set; }
public string UserId { get; set; }
public string Url { get; set; }
}
Configuration will look like this:
<filter type="Namespace.UserRequestFilter, Assembly">
<userId value="TESTUSER"/>
<url value="/foo/foobar.aspx"/>
<acceptOnMatch value="true"/>
</filter>
Also you can create compound filter, but I didn't find way to use it in configuration. Looks like it could be attached only programmatically (which is useless ^_^):
IAppenderAttachable logger = (IAppenderAttachable)_log.Logger;
AppenderSkeleton appender = (AppenderSkeleton)logger.GetAppender("appenderName");
CompoundFilter compoundFilter = new CompoundFilter();
compoundFilter.AddFilter(new PropertyFilter() { Key = "UserId", StringToMatch = "TEST" });
compoundFilter.AddFilter(new PropertyFilter() { Key = "Url", StringToMatch = #"/foo/foobar.aspx" });
appender.AddFilter(compoundFilter);
logger.AddAppender(appender);
[UPDATE]
Here is trick that you can use - create filter, which will check all filters down the filters chain:
public class DenyAllSubsequentFilter : FilterSkeleton
{
public override FilterDecision Decide(LoggingEvent loggingEvent)
{
IFilter nextFilter = Next;
if (nextFilter == null)
return FilterDecision.Accept;
while (nextFilter != null)
{
if (nextFilter.Decide(loggingEvent) != FilterDecision.Deny)
return FilterDecision.Accept;
nextFilter = nextFilter.Next;
}
return FilterDecision.Deny;
}
}
Usage:
<filter type="Namespace.DenyAllSubsequentFilter, Assembly"/>
<filter type="log4net.Filter.PropertyFilter">
<key value="UserId" />
<stringToMatch value="TEST" />
<acceptOnMatch value="false" />
</filter>
<filter type="log4net.Filter.PropertyFilter">
<key value="Url" />
<stringToMatch value="/foo/foobar.aspx" />
<acceptOnMatch value="false" />
</filter>
This filter will deny logging message if all subsequent filters will deny it.

log4net: Logging two messages in one row in the database?

I'm trying to log the input and output of a particular method to the database. I'd like to have this information in separate columns. I've investigated the PatternLayout and it seems that it only caters for a single %message parameter, meaning that if you do:
log.Debug("This is a message");
then log4net sees "This is a message" as the message to be logged. I want to do something like:
log.Debug(request, response);
Is this possible using log4net? Keep in mind that my goal is to have "request" and "response" in separate columns.
Your PatternConverter way is a step in the right direction, though the use of the static Input and Output properties makes it all a bit shaky (thread-safety wise).
The trick here is to realize that the message parameter on logger.Debug(...) is object and that you can pass in whatever you like.
You could define a custom message type
public class InputOutput
{
public string Input {get;set;}
public string Output {get;set;}
}
and then let your converters read either property
public class InputPatternConverter : PatternConverter
{
protected override void Convert(System.IO.TextWriter writer, object state)
{
var msg = ((LoggingEvent)state).MessageObject as InputOutput;
if (msg != null)
writer.Write(msg.Input);
}
}
public class OutputPatternConverter : PatternConverter
{
protected override void Convert(System.IO.TextWriter writer, object state)
{
var msg = ((LoggingEvent)state).MessageObject as InputOutput;
if (msg != null)
writer.Write(msg.Output);
}
}
the logging then becomes much cleaner
logger.Debug(new InputOutput { Input = ..., Output = ...});
your config would be the same.
A tip though is to subclass the PatternLayout and add the converters in the constructor of that class. That way you can also trim down your config. This will not cause you to loose the %message token, your %input and %output tokens will come in addition to all the tokens that PatternLayout supports. So you could actually have a pattern like this:
"%date %message %newline%newline %input %newline%newline %output
Here's a quick implementation of a custom pattern layout:
public class InputOutputPatternLayout : PatternLayout
{
public InputOutputPatternLayout()
{
AddConverter("input", typeof(InputPatternConverter));
AddConverter("output", typeof(OutputPatternConverter));
}
}
I've come up with one way to do this using custom PatternConverters
public class InputPatternConverter : PatternConverter
{
private static string _input;
public static string Input
{
get { return _input; }
set { _input = value; }
}
protected override void Convert(System.IO.TextWriter writer, object state)
{
writer.Write(Input);
}
}
public class OutputPatternConverter : PatternConverter
{
private static string _output;
public static string Output
{
get { return _output; }
set { _output = value; }
}
protected override void Convert(System.IO.TextWriter writer, object state)
{
writer.Write(Output);
}
}
Appender Specification:
<appender name="ADONetAppender" type="log4net.Appender.AdoNetAppender">
<bufferSize value="1" />
<connectionType value="System.Data.SqlClient.SqlConnection, System.Data, Version=1.0.3300.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" />
<connectionString value="data source=servername;initial catalog=database;Integrated Security=SSPI;" />
<commandText value="INSERT INTO RequestLog ([input], [output]) VALUES (#input, #output)" />
<parameter>
<parameterName value="#input" />
<dbType value="String" />
<size value="4000" />
<layout type="log4net.Layout.PatternLayout">
<converter>
<name value="input" />
<type value="InputPatternConverter, ApplicationName" />
</converter>
<conversionPattern value="%input" />
</layout>
</parameter>
<parameter>
<parameterName value="#output" />
<dbType value="String" />
<size value="4000" />
<layout type="log4net.Layout.PatternLayout">
<converter>
<name value="output" />
<type value="OutputPatternConverter, ApplicationName" />
</converter>
<conversionPattern value="%output" />
</layout>
</parameter>
</appender>
Call it using:
InputPatternConverter.Input = inputString;
OutputPatternConverter.Output = outputString;
XmlConfigurator.Configure();
ILog logger = LogManager.GetLogger(typeof(ApplicationClassName));
logger.Debug("");

Resources