EventLog WriteEntry not does not write into EventViewer in C# - c#

I have created a C# application which creates custom log name in the event viewer, and writing entries into event viewer.
My application is working fine on Windows-7 OS machine But when i copied my binaries into another window-2012 server standard(SP1) machine its not working.
I ran my application RunAs administrator and I am seeing only custom log name but not the logging messages.
I have given full permission in the registry eventlog entry but no luck.
Could somebody suggest me how to fix the issue. Or debugging technique to fix this issue.
I have debugged the code there are no exceptions, Code is running smoothly.
my code is so simple like as below:
EventLog.CreateEventSource("MyApp", "TestingApplication")
EventLog.WriteEntry("MyApp", "Testing 123")
today i have tested with same eventsource and eventlogname, like as below
EventLog.CreateEventSource("MyApp", "MyApp")
it worked fine in window-2012 server standard(SP1), is there anything issue in Win-2012(SP1) or am I missing anything.

Even while running as an administrator, in Windows Server 2008 R2 it defaults to not having elevated privileges. You need an elevated privilege to create the source. Once the source is created, it should let you write to it.
There are a few ways to solve this, if you're building an actual application, you can modify the application manifest to require elevated privileges and admin rights:
<?xml version="1.0" encoding="utf-8" ?>
<asmv1:assembly manifestVersion="1.0" xmlns="urn:schemas-microsoft-com:asm.v1"
xmlns:asmv1="urn:schemas-microsoft-com:asm.v1"
xmlns:asmv2="urn:schemas-microsoft-com:asm.v2"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<assemblyIdentity version="1.0.0.0" name="MyApplication" />
<trustInfo xmlns="urn:schemas-microsoft-com:asm.v2">
<security>
<requestedPrivileges xmlns="urn:schemas-microsoft-com:asm.v3">
<requestedExecutionLevel level="requireAdministrator"
uiAccess="false" />
</requestedPrivileges>
</security>
</trustInfo>
</asmv1:assembly>
Or, again if you're building a normal application, you can split the code to create the event source into a separate process. Then you can check to see if the event source exists, if it does, just use it, if it does not, you can run your new process that creates the event source.
In the Process start info you'll need the 'runas' verb.
If you're running a windows service, you're a bit more restricted, as services do not have the capability of "elevating" privileges through their manifest directly as far as I'm aware.
However, your service installer can elevate just fine, and should be running under the needed admin rights, you can create your event source inside the service installer; and then just consume it within the service itself.

I solved this problem by Stop/Start Windows Event Log services. I dont know what was the problem but like Leo saied it can be a bug.

I was having exact same issue. I am using WinServer 2012r2. Log created successfully but writing events appear in the Application Log. I read a lot of MSDN and forums and done everything up to the books and still have this problem. Seems MS has a bug in latest .Net framework which causes all messages to go to the Application Log :(
Finally after I rebooted server everything was working fine. I am not sure why would I need to reboot server if testing with eventcreate command line everything was working fine.

Related

Security Exception in Web.Config On Server [duplicate]

I've encountered an error deploying a site to a server. When trying to load the home page, or access authentication on the new site in IIS, I get the error:
Config Error: This configuration section cannot be used at this path.
This happens when the section is locked at a parent level. Locking is
either by default (overrideModeDefault="Deny"), or set explicitly by a
location tag with overrideMode="Deny" or the legacy
allowOverride="false".
More detail can be found here, in Scenario 7 matches my hex error code.
The solution given on the linked site above is to set Allow for overrideModeDefault in the section mentioned in my error, in the applicationHost.config file. In my case, under Security in system.webServer. But if I look at the applicationHost.config on my local computer, where the site is properly deployed already, that section is set to Deny.
If this solution is correct, how is my local instance running just fine with the same web.config? According to my applicationHost.config, that section should be locked, but it's not. I'd prefer to not change the applicationHost.config file, because there are many other sites running on that server. Is there another solution?
I had the same problem. Don't remember where I found it on the web, but here is what I did:
Click "Start button"
in the search box, enter "Turn windows features on or off"
in the features window, Click: "Internet Information Services"
Click: "World Wide Web Services"
Click: "Application Development Features"
Check (enable) the features. I checked all but CGI.
btw, I'm using Windows 7. Many comments over the years have certified this works all the way up to Windows 10 and Server 2019, as well.
You could also use the IIS Manager to edit those settings.
Care of this Learn IIS article:
Using the Feature Delegation from the root of IIS:
You can then control each of machine-level read/write permissions, which will otherwise give you the overrideMode="Deny" errors.
For Windows Server 2012 and IIS 8, the procedure is similar.
The Web Server (IIS) and Application Server should be installed, and you should also have the optional Web Server (IIS) Support under Application Server.
Browse to “C:\Windows\System32\inetsrv\config” (you will need administrator rights here)
Open applicationHost.config
Note: In IISExpress and Visual Studio 2015 the applicationHost.config is stored in $(solutionDir).vs\config\applicationhost.config
Find the section that showed up in the “config source” part of the error message page. For me this has typically been “modules” or “handlers”
Change the overrideModeDefault attribute to be Allow
So the whole line now looks like:
<section name="modules" allowDefinition="MachineToApplication" overrideModeDefault="Allow" />
After saving the file, the page loaded up fine in my browser.
Warning:
Editing applicationHost.config on 64-bit Windows
You need to unlock handlers. This can be done using following cmd command:
%windir%\system32\inetsrv\appcmd.exe unlock config -section:system.webServer/handlers
Maybe another info for people that are getting this error on IIS 8, in my case was on Microsoft Server 2012 platform. I had spend couple of hours battling with other errors that bubbled up after executing appcmd. In the end I was able to fix it by removing Web Server Role and installing it again.
1. Open "Turn windows features on or off" by: WinKey+ R => "optionalfeatures" => OK
Enable those features under "Application Development Features"
Tested on Win 10 - But probably will work on other windows versions as well.
I ran these two commands from an elevated command prompt:
%windir%/system32/inetsrv/appcmd unlock config /section:anonymousAuthentication
%windir%/system32/inetsrv/appcmd unlock config /section:windowsAuthentication
As per my answer to this similar issue;
Try unlocking the relevant IIS configuration settings at server level, as follows:
Open IIS Manager
Select the server in the Connections pane
Open Configuration Editor in the main pane
In the Sections drop down, select the section to unlock, e.g. system.webServer > defaultPath
Click Unlock Attribute in the right pane
Repeat for any other settings which you need to unlock
Restart IIS (optional) - Select the server in the Conncetions pane, click Restart in the Actions pane
This Did the trick for me, for IIS 8 Windows server 2012 R2
Go to "Turn on Features"
Then go to all default setting , Next, Next, Next etc..
Then, select as shown below,
Then reset IIS (optional) but do it safer side.
This is an additional solution as its a generic problem everyone have different of problem and thus different solution. Cheers!
The best option is to Change Application Settings from the Custom Site Delegation
Open IIS and from the root select Feature Delegation and then select Application Settings and from the right sidebar select Read/Write
On Windows Server 2012 with IIS 8 I have solved this by enabling ASP.NET 4.5 feature:
and then following ken's answer.
To fix this open up the IIS Express applicationhost.config. This file is stored at C:\Users[your user name]\Documents\IISExpress\config\applicationhost.config
Update for VS2015+: config file location is $(solutionDir).vs\config\applicationhost.config
Look for the following lines
<section name="windowsAuthentication" overrideModeDefault="Deny" />
<section name="anonymousAuthentication" overrideModeDefault="Deny" />
<add name="WindowsAuthenticationModule" lockItem="true" />
<add name="AnonymousAuthenticationModule" lockItem="true" />
Change those lines to
<section name="windowsAuthentication" overrideModeDefault="Allow" />
<section name="anonymousAuthentication" overrideModeDefault="Allow" />
<add name="WindowsAuthenticationModule" lockItem="false" />
<add name="AnonymousAuthenticationModule" lockItem="false" />
Save it and refresh Asp.net Page.
In our case on IIS 8 we found the error was produced when attempting to view Authentication" for a site, when:
The server Feature Delegation marked as "Authentication - Windows" = "Read Only"
The site had a web.config that explicitly referenced windows authentication; e.g.,
Marking the site Feature Delegation "Authentication - Windows" = "Read/Write", the error went away. It appears that, with the feature marked "Read Only", the web.config is not allowed to reference it at all even to disable it, as this apparently constitutes a write.
Seems that with IIS Express and VS 2015, there's a copy of the applicationHost.config file at $(solutionDir).vs\config\applicationhost.config so you'll need to make changes there. See this link: http://digitaldrummerj.me/iis-express-windows-authentication/
Make sure these lines are changed per below:
<section name="windowsAuthentication" overrideModeDefault="Allow" />
<section name="anonymousAuthentication" overrideModeDefault="Allow" />
<add name="WindowsAuthenticationModule" lockItem="false" />
<add name="AnonymousAuthenticationModule" lockItem="false" />
In my case it was that on server was not enabled "HTTP Activation" under .NET Framework Features. So for Windows Server 2012 the solution which worked for me was:
Server Manager -> Add roles and features -> Features -> make sure that under .NET Framework of version you want to use is checked "HTTP Activation"
The Powershell way of enabling the features (Windows Server 2012 +) - trim as needed:
Install-WindowsFeature NET-Framework-Core
Install-WindowsFeature Web-Server -IncludeAllSubFeature
Install-WindowsFeature NET-Framework-Features -IncludeAllSubFeature
Install-WindowsFeature NET-Framework-45-ASPNET -IncludeAllSubFeature
Install-WindowsFeature Application-Server -IncludeAllSubFeature
Install-WindowsFeature MSMQ -IncludeAllSubFeature
Install-WindowsFeature WAS -IncludeAllSubFeature
The error says that the configuration section is locked at the parent level.
So it will not be directly 1 config file which will resolve the issue,
we need to go through the hierarchy of the config files to see the inheritance
Check the below link to go through the File hierarchy and inheritance in IIS
https://msdn.microsoft.com/en-us/library/ms178685.aspx
So you need to check for the app config settings in the below order
ApplicationHost.config in C:windows\system32\inetsrv\config. Change the overrideModeDefault attribute to be Allow.
ApplicationName.config or web.config in the applications directory
Web.config in the root directory.
Web.config in the specific website (My issue was found at this place).
Web.config of the root web (server's configuration)
machine.config of the machine (Root's web.config and machine.config can be found at - systemroot\MicrosoftNET\Framework\versionNumber\CONFIG\Machine.config)
Go carefully through all these configs in the order of 1 to 6 and you should find it.
I noticed one answer that was similar, but in my case I used the IIS Configured Editor to find the section I wanted to "unlock".
Then I copied the path and used it in my automation to unlock it prior to changing the sections I wanted to edit.
. "$($env:windir)\system32\inetsrv\appcmd" unlock config -section:system.webServer/security/authentication/windowsAuthentication
. "$($env:windir)\system32\inetsrv\appcmd" unlock config -section:system.webServer/security/authentication/anonymousAuthentication
I needed to change the SSL settings on a subfolder when i got this nice message. In my case following action helped me out.
Opened C:\Windows\System32\inetsrv\config\applicationHost.config
And changed the value from overrideModeDefault="Deny" to "Allow"
<sectionGroup name="system.webServer">
...
<sectionGroup name="security">
<section name="access" overrideModeDefault="Allow" />
</sectionGroup>
In my case, I got this error because I was operating on the wrong configuration file.
I was doing this:
Configuration config = serverManager.GetWebConfiguration(websiteName);
ConfigurationSection serverRuntimeSection = config.GetSection("system.webServer/serverRuntime");
serverRuntimeSection["alternateHostName"] = hostname;
instead of the correct code:
Configuration config = serverManager.GetApplicationHostConfiguration();
ConfigurationSection serverRuntimeSection = configApp.GetSection("system.webServer/serverRuntime", websiteName);
serverRuntimeSection["alternateHostName"] = hostname;
in other words, I was trying to operate on the website's web.config instead of the global file C:\Windows\System32\inetsrv\config\applicationHost.config, which has a section (or can have a section) for the website. The setting I was trying to change exists only in the applicationHost.config file.
In my case, it was something else.
When I loaded the solution in a new version of Visual Studio, VS apparently created a new project-specific applicationhost.config file:
MySolutionDir\.vs\config\applicationhost.config
It started using the settings from the new config, instead of my already customized global IIS Express settings.
(\Users\%USER%\Documents\IISExpress\config\applicationhost.config)
In my case this was the setting that needed to be set. Of course it could be something else for you:
<section name="ipSecurity" overrideModeDefault="Allow" />
Received this same issue after installing IIS 7 on Vista Home Premium. To correct error I changed the following values located in the applicationHost.config file located in Windows\system32\inetsrv.
Change all of the following values located in section -->
<div mce_keep="true"><section name="handlers" overrideModeDefault="Deny" /> change this value from "Deny" to "Allow"</div>
<div mce_keep="true"><section name="modules" allowDefinition="MachineToApplication" overrideModeDefault="Deny" /> change this value from "Deny" to "Allow"</div>
Can You try this:
Go to application path where you're getting deny error, right click
Properties->Security tab
In that, change the permissions and check the checkbox read and write. Then it will work without any error hopefully.
For Windows Server 2008 and IIS 7, the procedure is similar.
please refer to this:
http://msdn.microsoft.com/en-us/library/vstudio/bb763178(v=vs.100).aspx
in add role service, u will see "Application Development Features"
Check (enable) the features. I checked all.
In my case I was getting this error when attempting to update the authentication settings in IIS also in addition to browsing. I was able to remove this error by removing the authentication setting from the web.config itself. Removing a problematic configuration section may be less invasive and preferable in some cases than changing the server roles and features too much:
Section Removed:
<security>
<authentication>
<windowsAuthentication enabled="true" />
</authentication>
</security>
I had the similar issue, but I used the following powershell script which helped me to achieve above steps in on button click.
#Install IIS
Import-Module ServerManager
Add-WindowsFeature Web-Server, Web-Asp-Net45, Web-Mgmt-Console, Web-Scripting-Tools, NET-WCF-HTTP-Activation45, Web-Windows-Auth
the list of features can be added or removed based on the requirement.
I had an issue where I was putting in the override = "Allow" values (mentioned here already)......but on a x64 bit system.......my 32 notepad++ was phantom saving them. Switching to Notepad (which is a 64bit application on a x64 bit O/S) allowed me to save the settings.
See :
http://dpotter.net/technical/2009/11/editing-applicationhostconfig-on-64-bit-windows/
The relevant text:
One of the problems I’m running down required that I view and possibly edit applicationHost.config. This file is located at %SystemRoot%\System32\inetsrv\config. Seems simple enough. I was able to find it from the command line easily, but when I went to load it in my favorite editor (Notepad++) I got a file not found error. Turns out that the System32 folder is redirected for 32-bit applications to SysWOW64. There appears to be no way to view the System32 folder using a 32-bit app. Go figure.
Fortunately, 64-bit versions of Windows ship with a 64-bit version of Notepad. As much as I dislike it, at least it works.
I had the same issue.
Resolved it by enabling Application Server feature. Restarted iis
after that.
This worked for me
Also in IIS 8 you can solve this problem by changing the server to IIS Express. Goto debug->Properties
In the Web select the server as IIS Express from the dropdown and then rebuild the solution
To make a change at Application Level (Web.Config):
Please remove the Trust Level from the web.config:
Actually I was getting this error when I was trying to host my Website on the Hosting Server where I don't have control on their Server. Removing the above line from my Application web.config solved my issue.

Visual Studio IIS HTTP 503 Service Unavailable

I have searched every topic and it seems that everyone got there problem solved already and I have tried all the possible solution they have provided or I am still missing something.
FireWall already turned off.
Ok so I am trying to access my IIS server Application that is currently run by VS 2013. Update 2.
when I try to access http://localhost:29790/ <- this to http://192.168.1.3:29790 which is my current IP on my local. It gives me 503 error.
I have searched for the solution and gives my a tons of.
applicationhost.config file
<site name="SMApplication(4)" id="10">
<application path="/" applicationPool="Clr4IntegratedAppPool">
<virtualDirectory path="/" physicalPath="C:\Users\DanZe3\Documents\Visual Studio 2013\Projects\SMApplication\SMApplication" />
</application>
<bindings>
<binding protocol="http" bindingInformation="*:29790:localhost" />
</bindings>
</site>
I have tried this.
*:29790:*
*:29790:
:29790:
by chaining the bindings. But it is still not working. I restart the PC or the VS as well. But the problem is, when I try to re-open VS. VS overwrite the applicationHost.config and add another binding.
As you can see in my site named "SMApplication(4)" (It has made 4 already!) This is automatic when I re-open the file
and then the bindings will be *:29790:localhost again.
Somehow Editing this bindings. Visual Studio will create another binding of the application, having another site named SMApplication(5).
I have tried to remove the localhost on the *:29790:localhost, but when I run the application it gives me.
Unable to Launch IIS Express Web Server.
The start URL specified is not a valid. http://localhost:29270/
I had a similar problem with a similar solution:
Scenario: I had the app pool running under an user account and it's password had been changed because it had expired. I never updated IIS so the app pool would stop by itself every time when a try to run website. Even if I start AppPool manually, It stopped on website run.
Solution: update the credentials for the account on IIS or use another account
I
Application's DefaultAppPool (or other custom name) may have stopped for some reason. Make sure it is on. Recycle too.
I have faced this issue recently after Windows 10 update (Dev machine).
I have created a new IIS AppPool and assigned to the website and that fixed the issue.
You should try to remove IIS Express folder at %USERPROFILE%\Documents.
Next important thing is to restart Visual Studio and open it as admin.
Then you can right click your ASP.NET project and go into Properties view. There you can configure web settings for IIS hosting in the Web tab. Try to change IIS settings here and then save with Ctrl+S.
When there is a * next to the web tab because you tried to edit settings but could not save, you need to change to another tab, switch back to Web tab and then save with Ctrl+S. (The Properties view for ASP.NET apps in Visual Studio is still buggy ...)
I am currently using Visual Studio 2015 CE so I don't know whether my solution applies to your problem. Anyways try to remove any folders that got cached by IIS Express hosting and never really got cleaned up afterwards.
So, in my case the only thing that fixed this was setting the ip statically in a new binding protocol, in your case would be something like this:
<site name="SMApplication(4)" id="10">
<application path="/" applicationPool="Clr4IntegratedAppPool">
<virtualDirectory path="/" physicalPath="C:\Users\DanZe3\Documents\Visual Studio 2013\Projects\SMApplication\SMApplication" />
</application>
<bindings>
<binding protocol="http" bindingInformation="*:29790:localhost" />
<binding protocol="http" bindingInformation="*:29790:192.168.1.8" />
</bindings>
</site>
This won't cause any problem in a production environment.

User Account Control on OK Button

I have researched over the internet about the UAC functionality on the form OK button but couldn't get the information. All of the information related to implementing the UAC is they relaunch the application with the administrative privileges.
My requirement is to make the application in which when user click on OK button with Shield Icon on it, user will be able to save some information in the windows Registry through elevate the user privilege but I don't want to relaunch the application with administrator rights.
Relaunching the application (or launching a helper application) is what you do. It is the requesting of elevated privileges while launching an application that causes the UAC confirmation screen to appear. The purpose of showing the shield icon is to let the user know that confirmation screen is coming up, basically.
You don't have to just relaunch your application. If your application allows multiple instances, you can launch a second copy with command-line parameters indicating the registry change to make. Or you can have a helper application that does admin things, and launch that as needed. A helper application doesn't need to create or show a window; it can be an entirely background operation.
May be you should add an application manifest and require administrator rights:
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<assembly xmlns="urn:schemas-microsoft-com:asm.v1" manifestVersion="1.0">
<trustInfo xmlns="urn:schemas-microsoft-com:asm.v3">
<security>
<requestedPrivileges>
<requestedExecutionLevel level="requireAdministrator" uiAccess="false"/>
</requestedPrivileges>
</security>
</trustInfo>
</assembly>
Edit
It may not be the best solution here, because the entire application is running under elevated privileges and that can be a security vulnerability.

Application manifest, admin rights and autostart on Windows Vista\7

So, I have an app that needs admin rights to work.
I created an app.manifest file. It works perfectly.
But now I need to set my program to autostart in Windows, and I am having trouble with this. If my program has an app.manifest file - program does not load when windows starts, but when I delete the app.manifest file - it works well.
Here is code from the app.manifest file:
<?xml version="1.0" encoding="utf-8"?>
<asmv1:assembly manifestVersion="1.0" xmlns="urn:schemas-microsoft-com:asm.v1" xmlns:asmv1="urn:schemas-microsoft-com:asm.v1" xmlns:asmv2="urn:schemas-microsoft-com:asm.v2" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<assemblyIdentity version="1.0.0.0" name="update.app"/>
<trustInfo xmlns="urn:schemas-microsoft-com:asm.v2">
<security>
<requestedPrivileges xmlns="urn:schemas-microsoft-com:asm.v3">
<requestedExecutionLevel level="requireAdministrator" uiAccess="false" />
</requestedPrivileges>
</security>
</trustInfo>
<compatibility xmlns="urn:schemas-microsoft-com:compatibility.v1">
<application>
<supportedOS Id="{35138b9a-5d96-4fbd-8e2d-a2440225f93a}"/>-->
</application>
</compatibility>
</asmv1:assembly>
Thanks for your help.
It’s not running because when you request admin access, the user needs to authorize it as admin (click the UAC Allow button), if it’s not popping up, there should be something in the icon-tray where the user can load all blocked exe's by clicking through the icon to give admin rights. when you run without the manifest, it is running only because it defaults to running under standard access (non-admin access, basic user level rights) so you're app is running but without being able to make changes to the system or file/path access items that only admin elevated apps can do. Get me?
The question is, how do you get your exe to auto start with admin privileges if it was installed and initially ran with admin elevated privileges during its installation? There seems to be a few answers to this but i am still searching for which one(s) actually work and are reliable. Basically, if it was elevated once, it should continue to be elevated (automatically, without prompting user) when it’s auto-starting (right after reboots) - but it is NOT doing that, and that's the problem. Let me know if you make progress with this.
UPDATE: 2012.11.10:
I have found a solution, using windows task scheduler to
programmatically schedule as task (that being, to run an EXE during
reboot/startup) & you have the option to run it as an elevated app,
and I think you need to be elevated to begin with in order to be able
to schedule this elevated auto run entry in the windows task
scheduler. Seems weird I know, but MS has allowed for this (and only
this) as a way to auto elevate your apps during startup, and they
haven't provided another way to do this. So, use appropriate API's to
register a Windows Task Schedule which will run your app elevated.
I'm not sure but it's probably about the authentication of startup user. if you did not already, you may check there for detailed information.

How to grant rights to HTTP namespace on installed service?

I have written a Windows Service, that opens up a WCF service on port 8003 of the local machine. Or at least, it's supposed to. The service can't start up, because of the famous exception:
System.ServiceModel.AddressAccessDeniedException: HTTP could not register URL http://+:8003/TRConfigurationService/. Your process does not have access rights to this namespace (see http://go.microsoft.com/fwlink/?LinkId=70353 for details).
This problem is addressed here, with a very simple answer: run a command prompt in admin mode, and execute:
netsh http add urlacl url=http://+:8003/ user=DOMAIN\UserName
All very well and good, and I can do that manually on my local dev machine. But how do I do that on the client installation, without the user having to do anything other than click "Yes" (only once after/during installation) when prompted to run in Admin mode?
It sounds like you should be able to do it using WIX.
Looks like the answer is simply to make sure the service runs in admin mode, then there's no problem! For those who don't know, add an app.manifest file to the project, and set
<requestedExecutionLevel level="requireAdministrator" uiAccess="false" />

Categories