i want to return List<DictionaryClass<string,string>> from web service but getting error for IDictionary - asp.net

[WebMethod]
public List<DictionaryClass<string,string>> GetDataByModuleDictionary(string ModuleName)
{
return BAL_GeneralService.GetDataByModuleDictionary(ModuleName);
}
here i m getting the following error...
The type DictionaryClass`2[[System.String, mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089],[System.String, mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089]] is not supported because it implements IDictionary.

XMLSerializer cannot serialize a class that implements IDictionary. To work around this you need to implement System.Xml.Serialization.IXmlSerializable in your DictionaryClass. See here for further info.

Related

JetEntityFrameworkProvider setup in VS2017

After compiling my project in VS2017 15.9.21 for.NET 4.5, I got following error, what am I missing?
Unable to determine the provider name for provider factory of type 'JetEntityFrameworkProvider.JetProviderFactory'. Make sure that the ADO.NET provider is installed or registered in the application config.
Error Dialog
This is where the exception gets thrown:
JetConnection.ShowSqlStatements = True
Dim fac1 = New JetConnectionFactory()
Dim con1 = fac1.CreateConnection(ORMGlobal.ConnectionString)
Using db As ORM = New ORM(con1)
Dim model = db.People.Count()
' ...
End Using
This is my DbContext setup:
Partial Public Class ORM
Inherits DbContext
Public Sub New()
MyBase.New()
Database.SetInitializer(New NullDatabaseInitializer(Of ORM))
End Sub
Public Sub New(con As Common.DbConnection)
MyBase.New(con, True)
Database.SetInitializer(New NullDatabaseInitializer(Of ORM))
End Sub
' ...
End Class
This is my config.app
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<entityFramework>
<providers>
<provider invariantName="JetEntityFrameworkProvider" type="JetEntityFrameworkProvider.JetProviderServices, JetEntityFrameworkProvider" />
<provider invariantName="System.Data.SqlClient" type="System.Data.Entity.SqlServer.SqlProviderServices, EntityFramework.SqlServer"/>
</providers>
</entityFramework>
<system.data>
<DbProviderFactories>
<remove invariant="JetEntityFrameworkProvider"/>
<add invariant="JetEntityFrameworkProvider" name="Jet Entity Framework Provider" description="Jet Entity Framework Provider"
type="JetEntityFrameworkProvider.JetProviderFactory, JetEntityFrameworkProvider"/>
</DbProviderFactories>
</system.data>
</configuration>
Exception details:
Exception thrown: 'System.NotSupportedException' in EntityFramework.dll
5/20/2021 3:49:07 PM :dwl: :LV:: Error in ConnectToDSN: System.NotSupportedException: Unable to determine the provider name for provider factory of type 'JetEntityFrameworkProvider.JetProviderFactory'. Make sure that the ADO.NET provider is installed or registered in the application config.
at System.Data.Entity.Utilities.DbProviderFactoryExtensions.GetProviderInvariantName(DbProviderFactory factory)
at System.Data.Entity.Infrastructure.DependencyResolution.DefaultInvariantNameResolver.GetService(Type type, Object key)
at System.Data.Entity.Infrastructure.DependencyResolution.CachingDependencyResolver.<>c__DisplayClass4_0.<GetService>b__0(Tuple`2 k)
at System.Collections.Concurrent.ConcurrentDictionary`2.GetOrAdd(TKey key, Func`2 valueFactory)
at System.Data.Entity.Infrastructure.DependencyResolution.CachingDependencyResolver.GetService(Type type, Object key)
at System.Data.Entity.Infrastructure.DependencyResolution.ResolverChain.<>c__DisplayClass5_0.<GetService>b__0(IDbDependencyResolver r)
at System.Linq.Enumerable.WhereSelectArrayIterator`2.MoveNext()
at System.Linq.Enumerable.FirstOrDefault[TSource](IEnumerable`1 source, Func`2 predicate)
at System.Data.Entity.Infrastructure.DependencyResolution.ResolverChain.GetService(Type type, Object key)
at System.Data.Entity.Infrastructure.DependencyResolution.RootDependencyResolver.GetService(Type type, Object key)
at System.Data.Entity.Infrastructure.DependencyResolution.ResolverChain.<>c__DisplayClass5_0.<GetService>b__0(IDbDependencyResolver r)
at System.Linq.Enumerable.WhereSelectArrayIterator`2.MoveNext()
at System.Linq.Enumerable.FirstOrDefault[TSource](IEnumerable`1 source, Func`2 predicate)
at System.Data.Entity.Infrastructure.DependencyResolution.ResolverChain.GetService(Type type, Object key)
at System.Data.Entity.Infrastructure.DependencyResolution.CompositeResolver`2.GetService(Type type, Object key)
at System.Data.Entity.Infrastructure.DependencyResolution.DbDependencyResolverExtensions.GetService[T](IDbDependencyResolver resolver, Object key)
at System.Data.Entity.Utilities.DbConnectionExtensions.GetProviderInvariantName(DbConnection connection)
at System.Data.Entity.Internal.InternalConnection.get_ProviderName()
at System.Data.Entity.Internal.LazyInternalContext.get_ProviderName()
at System.Data.Entity.Internal.DefaultModelCacheKeyFactory.Create(DbContext context)
at System.Data.Entity.Internal.LazyInternalContext.InitializeContext()
at System.Data.Entity.Internal.InternalContext.Initialize()
at System.Data.Entity.Internal.InternalContext.GetEntitySetAndBaseTypeForType(Type entityType)
at System.Data.Entity.Internal.Linq.InternalSet`1.Initialize()
at System.Data.Entity.Internal.Linq.InternalSet`1.get_InternalContext()
at System.Data.Entity.Infrastructure.DbQuery`1.System.Linq.IQueryable.get_Provider()
at System.Linq.Queryable.Count[TSource](IQueryable`1 source)
at TheBoardRoom.LockView.SQLdbHandler.ConnectToDSN(String sConnDSN, ADOConnection& connDB)

Asp Mvc Model doesnt exist

Hello I am sure this has been asked before but I cannot find the resolution.
The model that gets used
public class TaskList : Base
{
public TaskModel taskModel;
public List<Task> tasks;
public TaskList(string role, int userId) : base(role, userId)
{
taskModel = new TaskModel(userId,role);
}
public void GetTasks(int skip,int take)
{
tasks = taskModel.GetTasks(skip, take);
}
}
The controller
public ActionResult TaskListPartial(int skip, int take)
{
var taskList = new TaskList(role,userId);
taskList.GetTasks(skip, take);
return View(taskList);
}
Web.config compilation part
<system.web>
<compilation>
<assemblies>
<add assembly="System.Web.Mvc, Version=5.2.3.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" />
</assemblies>
</compilation>
How do I get the Model to resolve so I can get intellisense?
It show error because in view you have only single model class in namespace and in logic you are going to get value from list of object class.
Please define your view model as:
#model List<wrikeMVCNoAuth.ViewModel.Tasks.TaskList>
I apologise for the mistake I was under the impression that I didnt have to compile the project each time to get the intellisense to display int he views.
Once I had a successful compile everything came back.

How to add custom HTTP headers to asmx call using SoapExtension in c#

I am trying to add a custom HTTP header to the asmx call by intercepting it using the ProcessMessage (SoapMessage message) by extending SoapExtension Class in c#.
The problem here is that all i have in ProcessMessage() is the SoapMessage, while i need to add a custom http header.
I tried doing this :
HttpContext.Current.Request.Headers.Add("X-MyHeader", "xyz");
but it doesn't work.
I resolved this using the following steps:
Build an assembly using the class
namespace HttpWebRequestTest {
public class CustomHttpRequestCreator : IWebRequestCreate
public CustomHttpRequestCreator() { }
public WebRequest Create(Uri uri) {
HttpWebRequest webRequest = Activator.CreateInstance(typeof(HttpWebRequest),
BindingFlags.CreateInstance | BindingFlags.Public |
BindingFlags.NonPublic | BindingFlags.Instance,
null, new object[] { uri, null }, null) as HttpWebRequest;
webRequest.Headers.Add("MyHeader:MyValue");
return webRequest;
}
}
}
Add the file to GAC and register in web.config
<system.net>
<webRequestModules>
<remove prefix="http:"/>
<remove prefix="https:"/>
<add prefix="http:" type="HttpWebRequestTest.CustomHttpRequestCreator, HttpWebRequestTest, Version=1.0.0.0, Culture=neutral, PublicKeyToken=2bc4ccb2cdedce53" />
<add prefix="https:" type="HttpWebRequestTest.CustomHttpRequestCreator, HttpWebRequestTest, Version=1.0.0.0, Culture=neutral, PublicKeyToken=2bc4ccb2cdedce53" />
</webRequestModules>
</system.net>

Implementing Rolling log file in ASP.NET and EntLib 5.0

I am using EntLib 5 to create rolling flat file. This does not use the Rolling Flat File trace listener but the Flat File Trace listener with some modifications in the code.
Rolling Flat File trace listener was not used becuase of issues with it's inherent design as such does not fit the requirement here.
Here is the configuration section:
<configuration>
<configSections>
<section name="loggingConfiguration" type="Microsoft.Practices.EnterpriseLibrary.Logging.Configuration.LoggingSettings, Microsoft.Practices.EnterpriseLibrary.Logging, Version=5.0.414.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" requirePermission="true"/>
<sectionGroup name="system.web.extensions" type="System.Web.Configuration.SystemWebExtensionsSectionGroup, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35">
<sectionGroup name="scripting" type="System.Web.Configuration.ScriptingSectionGroup, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35">
<section name="scriptResourceHandler" type="System.Web.Configuration.ScriptingScriptResourceHandlerSection, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" requirePermission="false" allowDefinition="MachineToApplication"/>
<sectionGroup name="webServices" type="System.Web.Configuration.ScriptingWebServicesSectionGroup, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35">
<section name="jsonSerialization" type="System.Web.Configuration.ScriptingJsonSerializationSection, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" requirePermission="false" allowDefinition="Everywhere"/>
<section name="profileService" type="System.Web.Configuration.ScriptingProfileServiceSection, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" requirePermission="false" allowDefinition="MachineToApplication"/>
<section name="authenticationService" type="System.Web.Configuration.ScriptingAuthenticationServiceSection, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" requirePermission="false" allowDefinition="MachineToApplication"/>
<section name="roleService" type="System.Web.Configuration.ScriptingRoleServiceSection, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" requirePermission="false" allowDefinition="MachineToApplication"/>
</sectionGroup>
</sectionGroup>
</sectionGroup>
</configSections>
<loggingConfiguration name="" tracingEnabled="true" defaultCategory="General">
<listeners>
<add name="Flat File Trace Listener" type="Microsoft.Practices.EnterpriseLibrary.Logging.TraceListeners.FlatFileTraceListener, Microsoft.Practices.EnterpriseLibrary.Logging, Version=5.0.414.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
listenerDataType="Microsoft.Practices.EnterpriseLibrary.Logging.Configuration.FlatFileTraceListenerData, Microsoft.Practices.EnterpriseLibrary.Logging, Version=5.0.414.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
fileName="./Logs/MyLog.log" formatter="Text Formatter" />
</listeners>
<formatters>
<add type="Microsoft.Practices.EnterpriseLibrary.Logging.Formatters.TextFormatter, Microsoft.Practices.EnterpriseLibrary.Logging, Version=5.0.414.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
template="Timestamp: {timestamp(local)}{newline}
Message: {message}{newline}
Category: {category}{newline}
Priority: {priority}{newline}
EventId: {eventid}{newline}
Severity: {severity}{newline}
Title:{title}{newline}
Machine: {localMachine}{newline}
App Domain: {localAppDomain}{newline}
ProcessId: {localProcessId}{newline}
Process Name: {localProcessName}{newline}
Thread Name: {threadName}{newline}
Win32 ThreadId:{win32ThreadId}{newline}
Extended Properties: {dictionary({key} - {value}{newline})}"
name="Text Formatter" />
</formatters>
<categorySources>
<add switchValue="All" name="General">
<listeners>
<add name="Flat File Trace Listener" />
</listeners>
</add>
</categorySources>
<specialSources>
<allEvents switchValue="All" name="All Events" />
<notProcessed switchValue="All" name="Unprocessed Category" />
<errors switchValue="All" name="Logging Errors & Warnings">
<listeners>
<add name="Flat File Trace Listener" />
</listeners>
</errors>
</specialSources>
</loggingConfiguration>
<appSettings>
<add key="LogFolder" value="./Logs"/>
<add key="LogFileName" value="MyLog.log"/>
</appSettings>
<system.web>
Then I create a static class with the following code to configure the logging process:
public static class LoggingHelper {
private static string date = String.Format(System.Globalization.CultureInfo.InvariantCulture, "{0:yyyyMMdd}", DateTime.Now);
public static void SetLogFile(string logFileFolder,string logFileName) {
if (!string.IsNullOrEmpty(logFileName)) {
string strfileName = logFileFolder + "/" + date + logFileName;
LoggingHelper.SetTraceLogPath(strfileName, "FlatFile TraceListener", "General", "My Login System");
LoggingHelper.WriteLogFile("General", "Log file path " + logFileFolder + "/" + logFileName, " My Login System");
}
}
public static void SetTraceLogPath(string logFile, string logFileName, string category, string message) {
ConfigurationFileMap configFileMap = new ConfigurationFileMap();
configFileMap.MachineConfigFilename = "Web.config";
Configuration entLibConfig = WebConfigurationManager.OpenWebConfiguration("/ASP.NET_Logging");
LoggingSettings loggingSettings = (LoggingSettings)entLibConfig.GetSection(LoggingSettings.SectionName);
FlatFileTraceListenerData data = loggingSettings.TraceListeners.Get("Flat File Trace Listener") as FlatFileTraceListenerData;
data.FileName = logFile;
entLibConfig.Save();
LogEntry objLog = new LogEntry();
objLog.TimeStamp = System.DateTime.Now;
objLog.Categories.Add(category);
objLog.Message = message;
objLog.Priority = 1;
Logger.Write(objLog);
}
public static void WriteLogFile(string category, string msg, string title) {
try {
LogEntry le = new LogEntry();
le.TimeStamp = System.DateTime.Now;
le.Categories.Add(category);
le.Severity = TraceEventType.Information;
le.Priority = 1;
le.Message = msg;
le.Title = title;
le.Priority = 1;
Logger.Write(le);
} catch (LoggingException ex) {
LoggingHelper.WriteLogFile("General", "Error in writing log file " + ex.ToString(), "My Login System");
}
}}
The SetLogFile is called in the Application_Start method is Global.aspx page like so:
void Application_Start(object sender, EventArgs e)
{
// Code that runs on application startup
LoggingHelper.SetLogFile(ConfigurationSettings.AppSettings["LogFolder"].Trim (), ConfigurationSettings.AppSettings["LogFileName"].Trim());
LoggingHelper.WriteLogFile("General", "*** Application_Start ***", "");
}
Then I just call the WriteLogFile method wherever logging is required like so:
protected void btn_Page2_Click(object sender, EventArgs e) {
LoggingHelper.WriteLogFile("General", "btn_Page2_Click on Default.aspx", "");
Response.Redirect("SecongPage.aspx");
}
This works fine but for a minor issue. When the webpage is accessed for the first time it does not create the log file with Logfilename.log format, it just creates MyLog.log or whatever name is specified in Web.config and the log file is created outside the specified folder, in this case 'Log', for the first time. For consequent requests it will create the desired file with the desired filename in the configured folder.
This pattern is repeated on daily basis, the next day for the first request, the log will be written in yesterday's file and from the next request onwards a new file with today's date is created and all the log messages will be written in that file.
What did I do in my code which is causing this to happen?
We use the above code, with minor changes, for logging in our WinForms applications with no issues.
The website is currently not deployed at IIS. Since it's a work in progress, we are using the built-in development server from VS 2008. Another casue of concern is that the code tries to update the Web.config, at leat once, during startup. Don't know if this is the way to go about it!
This is a very simple sample with two pages, the first page has a button when clicked directs to second page. I am trying to have log file implemented on daily basis and the Rolling Flat File that comes with EntLib does not fit the requirement as it will not create a file on daily basis with the required date for that day.
Regards.
after searching for a long time. This is what I came up with. The idea is to use a FlatFile Trace listner instead of the in-build Rolling Flat File.
Disclaimer:
One of my colleagues had this code with him, he does not remember from where he got it, so if you are the owner or have written code similar to this, then thanks to you.
Please don't vote for this answer as I am not the owner of it. Nevertheless it's here to help anyone looking for similar implementation
This uses EntLib 4, but can be used with EntLib 5 as well.
Here is the logging part from my Web.config:
<loggingConfiguration name="Logging Application Block" tracingEnabled="true"
defaultCategory="User" logWarningsWhenNoCategoriesMatch="true">
<listeners>
<add fileName="trace.log" header="----------------------------------------"
footer="----------------------------------------" formatter=""
listenerDataType="Microsoft.Practices.EnterpriseLibrary.Logging.Configuration.FlatFileTraceListenerData, Microsoft.Practices.EnterpriseLibrary.Logging, Version=4.1.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
traceOutputOptions="None" filter="All" type="Microsoft.Practices.EnterpriseLibrary.Logging.TraceListeners.FlatFileTraceListener, Microsoft.Practices.EnterpriseLibrary.Logging, Version=4.1.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
name="FlatFile TraceListener" />
<add fileName="MyLoggingSystem.log" header="" footer="" formatter="Text Formatter"
listenerDataType="Microsoft.Practices.EnterpriseLibrary.Logging.Configuration.FlatFileTraceListenerData, Microsoft.Practices.EnterpriseLibrary.Logging, Version=4.1.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
traceOutputOptions="None" filter="All" type="Microsoft.Practices.EnterpriseLibrary.Logging.TraceListeners.FlatFileTraceListener, Microsoft.Practices.EnterpriseLibrary.Logging, Version=4.1.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
name="MyLoggingApp FlatFile TraceListener " />
<add listenerDataType="Microsoft.Practices.EnterpriseLibrary.Logging.Configuration.WmiTraceListenerData, Microsoft.Practices.EnterpriseLibrary.Logging, Version=4.1.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
traceOutputOptions="None" filter="All" type="Microsoft.Practices.EnterpriseLibrary.Logging.TraceListeners.WmiTraceListener, Microsoft.Practices.EnterpriseLibrary.Logging, Version=4.1.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
name="WMI TraceListener" />
</listeners>
<formatters>
<add template="[{timestamp}] [{category}] {message}" type="Microsoft.Practices.EnterpriseLibrary.Logging.Formatters.TextFormatter, Microsoft.Practices.EnterpriseLibrary.Logging, Version=4.1.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
name="Text Formatter" />
</formatters>
<categorySources>
<add switchValue="All" name="Developer">
<listeners>
<add name="FlatFile TraceListener" />
</listeners>
</add>
<add switchValue="All" name="General" />
<add switchValue="All" name="Instrumentation">
<listeners>
<add name="WMI TraceListener" />
</listeners>
</add>
<add switchValue="All" name="EskoLoggingApp">
<listeners>
<add name="MyLogging FlatFile TraceListener " />
</listeners>
</add>
<add switchValue="All" name="User">
<listeners>
<add name="FlatFile TraceListener" />
</listeners>
</add>
</categorySources>
<specialSources>
<allEvents switchValue="All" name="All Events" />
<notProcessed switchValue="All" name="Unprocessed Category" />
<errors switchValue="All" name="Logging Errors & Warnings">
<listeners>
<add name="FlatFile TraceListener" />
</listeners>
</errors>
</specialSources>
</loggingConfiguration>
I have two more sections, file name and folder to save the file:
<appSettings>
<add key="LogFolder" value="./Logs" />
<add key="LogFileName" value="MyLoginSystem" />
</appSettings>
Create a class like so:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Configuration;
using Microsoft.Practices.EnterpriseLibrary.Logging.Configuration;
using Microsoft.Practices.EnterpriseLibrary.Logging;
using System.Diagnostics;
using System.Globalization;
using Microsoft.Practices.EnterpriseLibrary.Logging.Formatters;
using Microsoft.Practices.EnterpriseLibrary.Logging.TraceListeners;
using Microsoft.Practices.EnterpriseLibrary.Logging.Filters;
/// <summary>
/// Summary description for Utility
/// </summary>
public static class Utility {
/// <summary>
/// Instance for log writer
/// </summary>
private static LogWriter writer;
/// <summary>
/// Instance for log writer
/// </summary>
private static LogWriter wmiWriter;
/// <summary>
/// Instance for log writer
/// </summary>
private static LogWriter eskoLoggingWriter;
/// <summary>
/// Intialize the Trace listners without using app.config file
/// </summary>
/// <param name="fileName">log file name</param>
public static void Initialize(string fileName) {
CreateLogWriterFromCode(fileName);
}
/// <summary>
/// Log User Messages
/// </summary>
/// <param name="message">Message text</param>
/// <param name="prior">Logger Priority</param>
/// <param name="traceType">Trave event type</param>
public static void LogUserMessage(string message, int prior, System.Diagnostics.TraceEventType traceType) {
LogEntry(message, "User", prior, traceType);
}
/// <summary>
/// Any type of Log Entry
/// </summary>
/// <param name="message">Message text</param>
/// <param name="category">Logger category</param>
/// <param name="prior">Priory logger </param>
/// <param name="traceType">Trace event type</param>
public static void LogEntry(string message, string category, int prior, System.Diagnostics.TraceEventType traceType) {
try {
LogEntry log = null;
log = new LogEntry();
log.Message = message;
log.Categories.Add(category);
log.Severity = traceType;
log.Priority = prior;
log.TimeStamp = DateTime.Now;
if (writer != null) {
writer.Write(log);
} else {
eskoLoggingWriter.Write(log);
}
// wmiWriter.Write(log);
} catch (LoggingException ex) {
LogEntry(ex.Message, "Developer", prior, traceType);
}
}
/// <summary>
/// Log Developer Messages
/// </summary>
/// <param name="message">Message text</param>
/// <param name="prior">Logger priority</param>
/// <param name="traceType">Trace event type</param>
public static void LogDeveloperMessage(string message, int prior, System.Diagnostics.TraceEventType traceType) {
LogEntry(message, "Developer", prior, traceType);
}
/// <summary>
/// Close splash screen
/// </summary>
public static void ExitSplash() {
Logger.Reset();
}
/// <summary>
/// Disposing log write instance
/// </summary>
public static void Dispose() {
if (writer != null) {
writer = null;
}
}
/// <summary>
/// Creating log writer programatically
/// </summary>
/// <param name="fileName">Full path of log file</param>
static private void CreateLogWriterFromCode(string fileName) {
if (writer != null) {
writer = null;
}
//// The formatter is responsible for the look of the message. Notice the tokens: {timestamp}, {newline}, {message}, {category}
TextFormatter formatter = new TextFormatter
("[{timestamp}]" + "[{category}]" + "{message}{newline}");
//// Log messages to a log file. Use the formatter mentioned above specified.
FlatFileTraceListener logFileListener = new FlatFileTraceListener(fileName, "", "", formatter);
//// My collection of TraceListeners.
LogSource mainLogSource =
new LogSource("FlatFile TraceListener", SourceLevels.All);
mainLogSource.Listeners.Add(logFileListener);
//// Assigning a non-existant LogSource for Logging Application Block
//// Specials Sources I don't care about.Used to say "don't log".
LogSource nonExistantLogSource = new LogSource("Empty");
//// I want all messages with a category of "User" or "Developer" to get distributed
//// to all TraceListeners in my mainLogSource.
IDictionary<string, LogSource> traceSources =
new Dictionary<string, LogSource>();
traceSources.Add("User", mainLogSource);
traceSources.Add("Developer", mainLogSource);
// Let's glue it all together.
// No filters at this time.
// I won't log a couple of the Special
// Sources: All Events and Events not
// using "Error" or "Debug" categories.
writer = new LogWriter(new ILogFilter[0],
traceSources,
nonExistantLogSource,
nonExistantLogSource,
mainLogSource,
"ALL",
false,
true);
}
/// <summary>
/// Creating log writer programatically
/// </summary>
static public void CreateLogWriterFromCode(string folder, string fileName) {
if (eskoLoggingWriter == null) {
//// The formatter is responsible for the look of the message. Notice the tokens: {timestamp}, {newline}, {message}, {category}
TextFormatter formatter = new TextFormatter
("[{timestamp}]" + "[{category}]" + "{message}{newline}");
//// Log messages to a log file. Use the formatter mentioned above specified.
string date = String.Format("{0:yyyyMMdd}", DateTime.Now);
FlatFileTraceListener logFileListener = new FlatFileTraceListener(folder + "/" + fileName + "_" + date + ".log", "", "", formatter);
WmiTraceListener wmiListener = new WmiTraceListener();
//// My collection of TraceListeners.
LogSource mainLogSource =
new LogSource("FlatFile TraceListener", SourceLevels.All);
LogSource wmiMainLogSource = new LogSource("WmiTraceListener", SourceLevels.All);
mainLogSource.Listeners.Add(logFileListener);
wmiMainLogSource.Listeners.Add(wmiListener);
//// Assigning a non-existant LogSource for Logging Application Block
//// Specials Sources I don't care about.Used to say "don't log".
LogSource nonExistantLogSource = new LogSource("Empty");
//// I want all messages with a category of "User" or "Developer" to get distributed
//// to all TraceListeners in my mainLogSource.
IDictionary<string, LogSource> traceSources =
new Dictionary<string, LogSource>();
traceSources.Add("User", mainLogSource);
traceSources.Add("Developer", mainLogSource);
IDictionary<string, LogSource> wmiTraceSources =
new Dictionary<string, LogSource>();
wmiTraceSources.Add("Instrumentation", wmiMainLogSource);
// Let's glue it all together.
// No filters at this time.
// I won't log a couple of the Special
// Sources: All Events and Events not
// using "Error" or "Debug" categories.
eskoLoggingWriter = new LogWriter(new ILogFilter[0],
traceSources,
nonExistantLogSource,
nonExistantLogSource,
mainLogSource,
"ALL",
false,
true);
wmiWriter = new LogWriter(new ILogFilter[0],
wmiTraceSources,
nonExistantLogSource,
nonExistantLogSource,
wmiMainLogSource,
"All",
true,
true);
}
}
}
In Global.asax, add these line in Application_Start
void Application_Start(object sender, EventArgs e) {
// Code that runs on application startup
Utility.CreateLogWriterFromCode(ConfigurationSettings.AppSettings["LogFolder"].Trim(), ConfigurationSettings.AppSettings["LogFileName"].Trim());
Utility.LogUserMessage("******** Application_Start Event My Login application started ******", 1, System.Diagnostics.TraceEventType.Information);
}
Now wherver you want to log a message, use
Utility.LogUserMessage
You might have to make minor changes to the Utility class if EntLib 5 is used. That should be trivial.
So there you have, a log file on daily basis with correct data for that day.
Again thank you to the anonymus person who wrote the Utility class in the first place.
Hope this helps.
Regards.

MVC3 Helper Not Working, Not Found

I created the following helper in my main directory under /Helpers:
HtmlHelpers.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Mvc.Html;
namespace Website.Helpers
{
public static class HtmlHelpers
{
public static MvcHtmlString ActiveActionLinkHelper(this HtmlHelper Html, string text, string action, string controller, string activeClass = "active", bool actionCheck = false)
{
if (Html.ViewContext.RouteData.GetRequiredString("controller") == controller)
{
if (actionCheck)
{
if (Html.ViewContext.RouteData.GetRequiredString("action") == action)
return Html.ActionLink(text, action, controller, new { Class = activeClass });
}
else
{
return Html.ActionLink(text, action, controller, new { Class = activeClass });
}
}
return Html.ActionLink(text, action, controller);
}
}
}
I added the namespace to the Views web.config in my Publishers Areas folder:
<system.web.webPages.razor>
<host factoryType="System.Web.Mvc.MvcWebRazorHostFactory, System.Web.Mvc, Version=3.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" />
<pages pageBaseType="System.Web.Mvc.WebViewPage">
<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="Website.Helpers" />
</namespaces>
</pages>
</system.web.webPages.razor>
I keep getting this message:
Compiler Error Message: CS1061: 'System.Web.Mvc.HtmlHelper' does not contain a definition for 'ActiveActionLink' and no extension method 'ActiveActionLink' accepting a first argument of type 'System.Web.Mvc.HtmlHelper' could be found (are you missing a using directive or an assembly reference?)
Core: #Html.ActiveActionLink("Dashboard", "Index", "Dashboard")
Does anyone know what I am doing wrong? There are hardly any tutorials on how or where to store an HTML helper. Can someone please advise me?
Core: #Html.ActiveActionLink("Dashboard", "Index", "Dashboard") // here it is the problem
as your method is ActiveActionLinkHelper, your calling different method.
#Html.ActiveActionLinkHelper("Dashboard", "Index", "Dashboard") // try like this.
The compiler is right. You're method is named ActiveActionLinkHelper. Change it to ActiveActionLink and all should be well

Resources